The simplest idea in machine learning: a new point is classified by asking its closest neighbours to vote. Drag the white star around, change k, and watch the decision regions reshape.
KNN has no training step. It just memorises the data. To predict, it (1) measures the distance from the query to every stored point, (2) sorts them, (3) keeps the closest k, and (4) takes a vote. Here are the nearest points to your star right now.
Highlighted rows are the k voters. Everything beyond them is ignored, however many points there are.
With k = 1 every training point is its own nearest neighbour, so training accuracy is a perfect (and misleading) 100%, and the regions are jagged islands around noise. A very large k averages over too much and washes out small classes. The honest test is leave-one-out: classify each point using all the others. Click the chart to try a k.
KNN trusts distance, so a feature measured in big numbers dominates. Imagine x₂ is switched to a unit that makes its numbers larger. The data is the same, but the neighbours change. This is why features are scaled before using KNN.
Adding new data is instant: just store it. Great for quick baselines.
No assumptions about a line or curve, so it can follow messy patterns.
Every query compares against all stored points, which gets expensive with big data.
Unscaled or irrelevant features distort distances and hurt accuracy.
Every noisy point carves its own island into the regions.
Small groups get outvoted and the boundary loses detail.
A stray pink point inside the cyan cluster fools k = 1. Then raise k to fix it.
Stretch x₂ and watch one feature take over the distance.