Two features, two classes 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
Each day now has two features: temperature across, sunny hours up. The red circles are days when the ice cream sold out, the blue squares days when it did not.
A new day 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
Tomorrow is expected to be 26 degrees with 6 hours of sun. The hollow circle is this day. Will the ice cream sell out?
The nearest 3 days 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
kNN measures the distance to every training day and picks the nearest 3. The lines go to these neighbours; the farthest one is 3 units away.
The vote: sold out 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
Of the 3 neighbours, 2 sold out and 1 did not. The majority says sold out; the new day became a red circle.
Regions with k = 1 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
Classifying every point of the chart the same way reveals the decision regions. With k = 1 each point takes the class of its single nearest day: the boundary is jagged, and one odd day builds its own little island.
Regions with k = 3 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
These are the regions with your k. As k grows the boundary smooths and odd days matter less; a very large k paints everything the majority class. Change k in the Try tab.
The scale problem 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
Distance is measured in each feature's own unit: 1 degree counts the same as 1 hour. If one feature's unit is much larger, it decides the distance. That is why features are usually brought to the same scale before kNN.
Why it matters 8 12 16 20 24 28 32 36 0 2 4 6 8 10 12 temperature, degrees sunny hours
kNN computes nothing in training and does all its work at prediction time. Finding similar users in recommenders and fetching the closest picture in image search rest on the same idea. As data grows, every prediction gets slower.