Skip to main content

Learn · k-Nearest Neighbours

k-nearest neighbours, live

The simplest classifier there is: to label a new point, look at the k closest labelled examples and take a vote. There's no training step — the data is the model. Hover the plot to classify the cursor, tune k, and click to add your own points. The shaded background is the decision boundary, computed with real Euclidean distance.

Hover to classify the cursor and see its k nearest neighbours. Click to add a point of the selected class.

Hover over the plot to classify a point.

5

Odd values only (1–9), so votes rarely tie. Small k = jagged boundary; large k = smoother.

Add points as

Every cell of the background is classified independently by a majority vote of its k nearest training points — that patchwork is the decision boundary. Nudge k and watch it tighten or relax.

How k-nearest neighbours works

Lazy & distance-based

To classify a new point, measure the Euclidean distance to every stored example, keep the k closest, and take the majority label. That's the whole algorithm.

No training step

Nothing is "learned" up front — the training set is the model. All the work happens at query time, which is why kNN is called a lazy learner.

The effect of k

Small k follows every point closely — a jagged, overfit boundary sensitive to noise. Large k averages over more neighbours, giving a smoother, more stable boundary.

Caveat: these points are illustrative, and real problems live in many more dimensions than two — where plain Euclidean distance suffers from the "curse of dimensionality" and needs scaling, weighting, or smarter indexes to stay useful. The intuition here still holds. More on the Learn shelf.