ClassificationSample

k-nearest neighbours

Classifies a point by the majority class among its k nearest neighbours.

Synthetic data, generated and trained in your browser.

Try this

  1. 01Set k = 1 on Moons: stray points get their own little islands.
  2. 02Turn on the test split and slide k from 1 to 40: test accuracy rises, then falls.
  3. 03Switch to Manhattan distance and compare the shape of the boundary.

How it works

  1. 1Store the labelled points.
  2. 2For a new point, find the k closest by the chosen distance.
  3. 3The share of each class among them is the predicted probability.

What the controls teach

Neighbours k
Neighbourhood size. Small k gives jagged, noise-sensitive boundaries; large k smooths them.
Distance
Euclidean measures straight-line distance; Manhattan sums horizontal and vertical steps, giving boxier regions.

Use it when

  • Irregular boundaries in low dimensions

Avoid it when

  • Many features (distances lose meaning)
  • Large training sets at prediction time

More in Classification