Concepts / k Nearest Neighbors Algorithm

k Nearest Neighbors Algorithm

The k-nearest-neighbors rule, abbreviated k-NN, is a classification rule for binary classification.

  • Programming
Interactive lab

Try it: k-Nearest Neighbours

How a k-nearest-neighbours classifier labels a new point: measure its distance to every known point, take the K closest, and let them vote.

How it works

  1. Measure the straight-line (Euclidean) distance from the new point to every labelled point.
  2. Rank the points from nearest to farthest.
  3. Keep the K nearest.
  4. Count how many of them belong to each class; the class with the most votes is the prediction (a tie goes to the class of the nearest tied neighbour).

Default run (15 steps): 10 labelled points and a new query point at (5, 3.5). K = 3. … Prediction: class A — A has the most votes.

Simplified: Small 2-D educational dataset (at most 30 points, three classes). Real KNN uses many features and usually scales them first.

Educational simulation

Loading the simulation…

From Data to a Label

The k-nearest-neighbors rule, abbreviated k-NN, is a classification rule for binary classification. It receives a training sample and a chosen value of k. For each point being considered, it returns one label by examining the labels of that point's k nearest neighbors.

Training samplelabeled pointsk nearest neighborsselected pointsPredicted labelmajority labelQuery pointpoint under considerationkchosen valueprovides pointsis classifiedselects how manylabels are counted
How do the training sample, query point, and chosen value of k enter the rule, and what output does the rule produce?

The Four Parts of a Decision

A training sample is the collection of labeled points supplied to the rule. The query point is the point currently being considered for classification. The rule identifies that point's nearest neighbors in the training sample and selects the first k neighbors in the ordering. Their labels are the neighbor labels used in the decision. The predicted label is the majority label among those selected labels.

PartRole in the rule
Training sampleThe labeled sample supplied to the rule
Query pointThe point being considered
Neighbor labelsThe labels of the selected k nearest neighbors
Predicted labelThe majority label returned by the rule

The objects involved in one k-NN classification decision

Counting the Neighbor Labels

Worked example

A majority of Red

The selected group of neighbors contains two Red labels and one Blue label. What label does the k-NN rule return?

  1. 1
    Select the labels

    The decision uses the labels of the selected neighbors, not labels from other points in the training sample.

  2. 2
    Count each class

    Red appears two times, while Blue appears one time.

  3. 3
    Choose the majority

    Red has the greater count among the selected labels.

Result

The predicted label is Red.

Selected neighborsk pointsRed2 labelsRed majoritygreater countRedpredicted labelBlue1 labelcountcount21return
How do the labels of the selected neighbors combine to produce the predicted label?

This is the complete decision step once the relevant neighbor labels are known. In binary classification, the labels come from two possible classes. Both labels may appear among the selected neighbors, and the rule chooses the one with the greater count.

What Changing k Changes

The parameter k controls how many neighbors contribute labels to the majority decision. Changing k can change the group being counted. Because a different number of neighbor labels may then be included, the majority label and the classification result can change.

Chosen kLabels usedPossible consequence
Smaller valueA smaller selected groupThe majority is based on fewer neighbor labels
Larger valueA larger selected groupAdditional neighbor labels may affect the majority

Changing k changes how many labels participate in the decision

Tracing an Unexpected Result

When a classification result seems unexpected, trace the rule in order. First check the chosen value of k. Next check which neighbors occupy the first k positions in the ordering. Then write down only their labels and recount them. An unexpected result can arise because changing k changes which labels are counted, or because the selected neighbor group or ordering is not the group you expected.

Training samplequery point and k alsosuppliedNeighbor orderingnearest to fartherFirst k neighborsselected groupNeighbor labelslabels to countMajority labelgreatest countPredicted labelreturned outputidentify neighborstake first kread labelscount classesreturn
As the rule runs from the input data to the predicted label, where could an unexpected classification result arise?

Common Tracing Mistakes

  • Common mistake

    Counting every point in the training sample

    What it looks like

    Using labels from all stored points instead of only the selected k nearest neighbors

    Why it fails

    Only the labels of the k nearest neighbors participate in the decision.

    Do this instead

    Write down the first k selected neighbors and count only their labels.

  • Common mistake

    Treating k as a class label

    What it looks like

    Reading k as if it were Red or Blue

    Why it fails

    k controls how many neighbors contribute labels; it is not one of the binary class labels.

    Do this instead

    Use k to select the group size, then count the labels in that group.

  • Common mistake

    Confusing the query point with a neighbor label

    What it looks like

    Assuming the point being classified already supplies the output label

    Why it fails

    The query point is the point under consideration; the output is determined by the labels of its selected neighbors.

    Do this instead

    Keep the query point separate from the neighbor labels and the final predicted label.

  • Common mistake

    Ignoring a changed value of k

    What it looks like

    Comparing two results while assuming both decisions counted the same number of neighbors

    Why it fails

    Changing k can change which labels are counted and therefore change the result.

    Do this instead

    Record k before comparing the selected groups or their majorities.

  • Common mistake

    Assuming the rule specifies how ties are resolved

    What it looks like

    Adding a tie-breaking procedure as though it were part of the definition

    Why it fails

    The rule specification does not provide implementation details for resolving a tie.

    Do this instead

    Recognize that tie resolution is outside the supplied rule specification.

Practice the Decision Rule

EASY

A query point has three selected nearest neighbors with labels Red, Blue, and Red. State the neighbor labels being counted and predict the output of the k-NN rule.

Hints
  • Count only the three selected labels.
  • Compare the number of Red labels with the number of Blue labels.

What do you think happens?

The selected labels are Red, Blue, and Red. What predicted label should the rule return?

  • Red

  • Blue

Reveal answer
Answer:

Red

Red appears twice and Blue appears once, so Red is the majority label among the selected neighbors.

Rule Summary

  1. 1
    The k-NN rule maps a training sample and a chosen k to a label for a point under consideration.
  2. 2
    Only the labels of the selected k nearest neighbors participate in the decision.
  3. 3
    The predicted label is the majority label among those selected neighbor labels.
  4. 4
    Changing k can change the selected group and therefore change the classification result.
  5. 5
    When tracing an unexpected result, check k, the neighbor ordering, the selected group, and the label count separately.

At a glance

  • k-NN accepts a training sample and a chosen value of k, then classifies each point under consideration.
  • The rule uses only the labels of the k selected nearest neighbors.
  • The label with the greater count becomes the predicted label in binary classification.
  • Changing k can change which labels are included and can therefore change the result.
  • An unexpected result should be traced through the selected neighbors, their ordering, k, and the final majority count.

Related concepts

More concepts starting with K