K-Nearest Neighbors
The Nearest Neighbor
Classification by proximity
k-NN classifies a point by looking at its closest neighbors. With k=1, the query point is assigned the class of its single nearest neighbor — here, class C at distance 0.39. Drag the sliders to move the query point and see how the nearest neighbor changes.
Step 1 of 6: The Nearest Neighbor
Classification by proximity
k-NN classifies a point by looking at its closest neighbors. With k=1, the query point is assigned the class of its single nearest neighbor — here, class C at distance 0.39. Drag the sliders to move the query point and see how the nearest neighbor changes.
Step 2 of 6: Choosing k
How many neighbors to ask
With k=3, the prediction takes a majority vote among 3 neighbors, balancing local detail with some smoothing.
Step 3 of 6: Distance Metrics
Euclidean vs. Manhattan
Euclidean distance measures the straight-line path — √(Δx² + Δy²). The equal-distance boundary is a circle. Toggle to Manhattan to see how grid-walk distance picks different neighbors.
Step 4 of 6: Decision Boundaries
The Voronoi landscape
With k=1, the boundary hugs every point — notice the isolated islands around outliers. Train accuracy is 100% but this overfits badly.
Step 5 of 6: Distance Weighting
Closer neighbors matter more
Uniform weighting: all 7 neighbors vote equally. The vote is purely a headcount — a distant neighbor counts the same as the closest one.
Step 6 of 6: The Curse of High Dimensions
When distance loses meaning
In 2D, the nearest point (0.30) is much closer than the farthest (1.62) — a 81% gap. k-NN can clearly tell neighbors apart.