k-Nearest Neighbors
Label something new by letting its nearest neighbours vote.
Drag the new fruit, or tap anywhere on the graph to place it. Its nearest fruits vote on what it is.
- apple
- pear
- new fruit: drag it, or tap the graph
- khow many nearest fruits vote
d = √((7.5 − 7.4)² + (8.5 − 8.2)²) = 0.32 cm to apple 1, the nearest
k = 3 nearest: 2 apples, 1 pear → apple
| 1 | 7.5 | 8.5 | apple | 0.32 | yes |
| 4 | 7.2 | 7.8 | apple | 0.45 | yes |
| 10 | 7.0 | 8.7 | pear | 0.64 | yes |
| 5 | 7.3 | 9.1 | pear | 0.91 | – |
| 13 | 8.0 | 7.5 | apple | 0.92 | – |
| 11 | 6.3 | 8.2 | pear | 1.10 | – |
| 6 | 7.2 | 7.1 | apple | 1.12 | – |
| 12 | 6.0 | 7.9 | pear | 1.43 | – |
| 14 | 8.5 | 7.1 | apple | 1.56 | – |
| 7 | 8.8 | 7.4 | apple | 1.61 | – |
| 2 | 6.6 | 10.0 | pear | 1.97 | – |
| 8 | 5.6 | 9.3 | pear | 2.11 | – |
| 9 | 5.3 | 8.5 | pear | 2.12 | – |
| 3 | 8.1 | 6.1 | apple | 2.21 | – |
Try it in Python
The same 14 fruits, new fruit and k as the Lab, in a few lines of Python. Plain Python measures every distance and counts the votes by hand.
What you are looking at
Each shape is a fruit we already know. Across the graph is its width; up the graph is its height, both in centimetres. Circles are apples and triangles are pears. Apples are round, so their width and height are close. Pears are taller than they are wide.
The diamond is a new fruit we have not labelled yet. Its colour shows what the model thinks it is. The numbers are made up, and Refresh Data shows another basket of 14 fruits.
What each symbol means
- q
- The new fruit, the one we want to label.
- x, y
- The width and height of a known fruit.
- d
- The distance from the new fruit to a known fruit: the length of the straight line between them.
- k
- How many of the nearest fruits get a vote.
- ŷ
- The prediction: apple or pear.
How the model decides
k-Nearest Neighbors follows a simple idea: things that look alike are usually the same kind. To label the new fruit, it takes three steps.
- Measure: find the distance from the new fruit to every known fruit.
- Pick the neighbours: keep the k fruits with the smallest distance. The dashed circle on the graph goes around them.
- Vote: each neighbour votes for its own kind, and the most votes wins.
That is all. The model does not learn a formula in advance. It simply remembers every fruit and looks at the closest ones each time a new fruit arrives.
Choosing k
With k = 1, only the single nearest fruit decides. One odd fruit, like a round pear, then claims the space around it. Turn on Areas to see the shaded region each kind would win, and watch the border wiggle around single fruits.
With a larger k, more fruits vote, so one odd fruit is outvoted and the border becomes smoother. But if k is too large, the vote reaches fruits far away, and small groups get drowned out.
To compare values of k fairly, the score Right on its own data hides each known fruit in turn and asks its k nearest neighbours to guess it. A fruit never votes for itself. We use odd values of k, so with two kinds of fruit the vote can never tie.
The model in short
- Remember every known fruit and its kind.
- Measure the distance from a new fruit to each of them.
- Vote: the k nearest fruits decide the new fruit's kind.
- Choose k by checking how often the model is right on fruits it has not seen.