A new point has no label. Who are its nearest neighbours, and what do they say? Before the clip plays, a question: makes no mistakes on the training dots; will it also do best on new dots?
Ask the nearest examples
100 labelled dots in two crescents; the plane is coloured by the vote of the k nearest.
100 training examples, each a dot at its two features: 50 of class A in one crescent, 50 of class B in the other.
Describe this picture
One plane, feature 1 across from −3 to 3 and feature 2 up from −2.25 to 2.25, at equal scales and with no tick labels. On it sit 100 training dots in two crescents that overlap at their tips: 50 round dots of class A and 50 squares of class B, with a key above the plane. The clip fades the dots in, class A first. A grey new point appears at (0, −0.1) and draws lines to its 7 nearest dots, nearest first, while a small tally beside it counts the classes: 3 of class A and 4 of class B, so the new point is labelled B. Then every point of the plane asks. The shading for wipes in from the left, each cell tinted with the class that wins its vote, and steps through 3, 7, 15 and 29, each fading into the next. The captions say what changes: at 1 stray dots get islands of their own, at 3 two islands are left, at 7 the islands are gone, and at 15 and 29 the boundary between the crescents grows smooth. Three readouts: “mistakes on the 100 training dots”, “mistakes on 100 new dots” (100 more dots from the same crescents, never drawn) and “neighbours asked, k”. After the clip a slider, “Neighbours, k”, takes odd values from 1 to 29 and starts at 29. With reduced motion the plane shows , 7 and 29 side by side.
Asking the nearest examples
Each example here is a point whose two coordinates are its two measurements, its features; Data as points in space builds that picture slowly. Its class, A or B, is its label. There are examples. Statistics writes for a sample size; on these pages counts examples.
To label the grey point, I look for the 7 training dots closest to it and count their labels. Four are B and three are A, so I call it B. That is the whole method: ask the nearest training examples, and take their vote.
“Nearest” needs a distance. Write example as the vector , with -th feature , and the new point as , with features . With features, the distance between them is the length of their difference, as in Length and the dot product:
Here . Sort the training examples by this distance and keep the first . I call their indices . If two examples sit at exactly the same distance, the one with the lower index goes first. With labels , the prediction (the hat marks a guess) is
With two classes and an odd , the vote can never tie. That is why the slider takes odd values only.
Distance treats every feature alike, so the units matter: a feature in millimetres outweighs one in metres. Data as points in space shows how to put features on one scale.
Decision regions and the boundary
Nothing stops me from asking the vote for every point of the plane, not only for the grey one. The instrument does that on a grid of 120 by 90 cells and tints each cell with the winning class. The cells that vote A form the decision region of A, the rest form the region of B, and the line where the two meet is the decision boundary.
Drag from 1 up to 29 and watch the regions. At each training dot claims the space nearest to it, so a stray dot inside the other crescent owns an island of its own. On these dots there are four. At two are left, from none, and by 29 the boundary is a smooth curve between the crescents.
A small follows every stray dot. A large averages over many dots and draws a smoother boundary, but past some size it starts to miss real detail in the shape. The number of neighbours asked is a choice the data do not make for me.
Why k = 1 makes no training mistakes
Now the question from the top of the page. Ask a training dot for its own label with , and its nearest training example is itself, at distance 0. So it always gets its own label back, and the training readout shows 0 mistakes. At the same readout shows 7. Judged on the training dots, looks perfect.
That proves nothing, because the method has seen those dots. The honest test is the second readout: 100 new dots from the same two crescents, which the method never saw. There makes 14 mistakes, the most of any on the slider; the clip’s other stops, 3, 7, 15 and 29, make 7 to 9. Some of those mistakes fall inside the islands that made the training count perfect: 5 of the 14.
Watch out
A low count of mistakes on the training examples does not mean a good model. Judge it on examples it has not seen.
A hundred new dots are too few to name a best : the counts for the larger differ by only a few dots, and a different hundred could put them in a different order. Bias and variance of a model explains why sits at the jumpy end, and Validation and cross-validation shows how to choose .
Predicting a number
The labels need not be classes. If each example carries a number, say a temperature, the nearest examples give their average instead of a vote:
The same choice of applies: a small follows each nearby reading, a large one smooths over many.
The cost of a prediction
There is no training step. The method keeps all the examples, and that is all it keeps: there is no formula inside it. The work comes at prediction time. To label one new point it computes distances, each a sum of squares, and finds the smallest. With a million examples, every single prediction means a million distances. Practical systems store the examples in search structures that skip most of them, but the data still have to be kept.
There is a second cost, in many features. Suppose the examples are spread evenly through a cube whose edges run from 0 to 1, one edge per feature. A smaller cube that holds 1 % of them needs edges of length , and that edge grows quickly with the number of features :
Describe this picture
A curve of the edge against the number of features , on a logarithmic axis from 1 to 100, with a dashed line at 1 for the whole range of a feature. Dots mark 0.01 at , 0.1 at , 0.631 at and 0.955 at ; there is no control.
With 100 features, the “neighbourhood” of 1 % spans almost the whole range of every feature. The nearest examples are then not near in any useful sense, and the vote says little.
Worked example
Six training dots: class A at , and ; class B at , and . The new point is . I list the dots by distance from it:
| Dot | Class | Squared distance | Distance |
|---|---|---|---|
| A | 1 | 1 | |
| B | 2 | 1.414 | |
| B | 4 | 2 | |
| A | 5 | 2.236 | |
| A | 8 | 2.828 | |
| B | 9 | 3 |
No two distances are equal, so the tie rule is not needed.
- : the nearest dot, , is A, so the answer is A.
- : the first three are A, B, B. B wins 2 to 1, so the answer is B.
- : add and , both A. A wins 3 to 2, so the answer is A.
One point, three answers.
Three answers for one point
The worked example: six dots, the new point (3, 3), and the vote of its 1, 3 and 5 nearest.
Six training dots: class A round, class B square.
Describe this picture
A plane with the six dots of the worked example, round for class A and square for class B, and the grey new point at . Beside it, the six dots are listed by distance from the new point, nearest first, with their class and distance. There is a play button and no other control. The dots fade in, class A first, then the new point appears. The clip steps through , 3 and 5. At each step a dashed circle grows round the new point out to its -th nearest dot, with radius 1, then 2, then 2.828; lines draw to the dots it takes in, nearest first, and each dot’s row in the list lights as it joins. Readouts count the class A and class B dots inside, with and the answer: 1 A; 1 A and 2 B; 3 A and 2 B. The new point’s label flips from A to B and back to A. With reduced motion the three steps are shown side by side.
Predicting a number. Three weather stations are nearest to a farm, and they read 10, 14 and 12 °C. With the prediction for the farm is their average, °C.
The cost. With examples and features, one prediction computes a million distances of two squared differences each.
Where you’ll meet this
Recommendations. “People whose ratings are nearest to yours liked this” is a nearest-neighbour vote, with each person a point whose features are their ratings.
Matching faults. A machine’s vibration is summarised as a few numbers and compared with records of past faults; the nearest past records name the likely fault.
Reading handwriting. Matching a scanned digit to its nearest stored examples is a common first method, a baseline that other methods must beat.
In signal processing · nearest-neighbour classification
Speech features in the signal processing course turns each slice of a vowel into a short vector of numbers. Its nearest-vowel rule labels a new slice by the stored vowel closest to it: a nearest-neighbour vote with .
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Distance | the same for every feature, so units matter | |
| Nearest examples | : the nearest training examples | equal distances: lower index first |
| Class | = most common , | odd : no tie between two classes |
| Number | the average of the neighbours | |
| Cost | distances of terms per prediction | no training; all data kept |
| Choice of | small: islands, follows noise; large: smooth | never misses a training example |
| Many features | cube holding 1 %: edge | 0.955 at |