← All AI/ML articles

K-Nearest Neighbors, visualised.

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.

1. Drag the query2. Watch neighbours vote3. Tune k and distance
class Aclass Bclass CDrag the ★ · drag a point · click empty space to add · double-click to remove
Query-
Radius to k-th neighbour0
Leave-one-out accuracy0
Points0
Behind the scenes · 1

How one prediction is made

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.

Behind the scenes · 2

Choosing k

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.


Cyan = training accuracy (cheats by seeing itself). Pink = leave-one-out accuracy. Star = best k. White line = your k.

Behind the scenes · 3

Distance depends on units

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.


Watch the main chart: the neighbourhood around the star stretches into an ellipse and the regions turn into horizontal bands.
Practical

When is KNN a good idea?

No training

Adding new data is instant: just store it. Great for quick baselines.

Any boundary shape

No assumptions about a line or curve, so it can follow messy patterns.

Slow at prediction

Every query compares against all stored points, which gets expensive with big data.

Needs scaling

Unscaled or irrelevant features distort distances and hurt accuracy.

Experiment

Try it yourself

k = 1: the jagged edge

Every noisy point carves its own island into the regions.

k = 25: too smooth

Small groups get outvoted and the boundary loses detail.

One mislabelled point

A stray pink point inside the cyan cluster fools k = 1. Then raise k to fix it.

Mismatched units

Stretch x₂ and watch one feature take over the distance.

Check yourself

Three quick questions

Key takeaways

  • KNN classifies a point by a vote among its k closest training points. It has no training step.
  • Small k gives jagged, noise-chasing boundaries; large k gives smooth but blurry ones.
  • Choose k using held-out or leave-one-out accuracy, not training accuracy.
  • Scale your features: distance is dominated by whichever feature has the biggest numbers.
  • Cost grows with data size because every prediction scans all stored points.
← Back to all articles