k-Nearest Neighbors Explorer
k-nearest neighbors classifies a point by letting its k closest training points vote — there is no training phase at all, the data is the model. Add points of three classes by clicking, or load presets (blobs, concentric rings, diagonal bands), and the background decision map recolours instantly. Hover anywhere and lines fan out from the cursor to its k nearest neighbours with the vote tally shown live. Slide k from 1 to 25 to morph the map from jagged islands that overfit every noisy point into smooth regions that may underfit, switch between Euclidean and Manhattan distance, and toggle distance-weighted voting. A button sweeps leave-one-out accuracy across every k and charts it, so you can find the sweet spot of the bias–variance trade-off.
Runs 100% in your browser — models are trained and computed locally on your device.
Read the full guide to this tool
Notes
- At k = 1 the map is the Voronoi diagram of the training set: every training point is classified perfectly — the textbook picture of overfitting.
- Leave-one-out cross-validation classifies each point using all the others; with distance weighting each neighbour’s vote counts 1/d, so near points dominate far ones.
- Manhattan distance sums coordinate differences (|Δx| + |Δy|) instead of the Euclidean straight line, so neighbourhoods become diamonds rather than circles and boundaries turn axis-aligned.
- Odd k avoids two-class ties, and asymptotically the error of 1-NN is at most twice the Bayes-optimal error (Cover & Hart, 1967).
- Runs 100% in your browser — models are trained and computed locally on your device.