LearnMachine learning

k-nearest neighbours

Classify a new point by asking its closest neighbours to vote.

  • Machine learning
  • Supervised
  • Classification

Drag the diamond to classify a new point. Click empty space to add points in the chosen colour.

How it works

No formula, just the neighbours.

k-nearest neighbours does not learn an equation. It simply remembers every example, and to classify something new it finds the k closest examples and lets them vote.

A small k listens to the closest few points, so it follows noise and odd points (overfitting). A large k averages over many points, which is smoother but can blur real detail (underfitting). Choosing k is a trade-off.

Because it compares each new point with every stored example, it slows down as the data grows, and its answers depend on how distance is measured.