k-Nearest Neighbours: 3 Steps to the Simplest Classifier

⚡ TL;DR: k-nearest neighbours classifies a new point by looking at the k closest training points and taking a vote. There is no training step at all — the model simply stores the data. It is the simplest algorithm in machine learning, and it is also the one that makes feature scaling matter most.

Every other model on this site learns parameters. k-nearest neighbours learns nothing. It memorises the training set and answers new questions by looking things up — and it still works remarkably well.

How k-nearest neighbours works

k-nearest neighbours diagram: the new point's five nearest neighbours are four Class B and one Class A, so the vote predicts Class B
With k = 5, four of the star’s five nearest neighbours are Class B, so k-nearest neighbours predicts Class B.

Three steps, and that is the whole algorithm:

1
Measure the distance from the new point to every training point
2
Keep the k closest ones
3
Take the majority class — or the average, for regression

Distance is usually Euclidean — the straight-line distance you get from Pythagoras, generalised to any number of dimensions:

$$d(p,q) = \sqrt{\sum_{i=1}^{n}(p_i – q_i)^2}$$

That is exactly the L2 norm of the difference between two vectors. If your features are text or high-dimensional, cosine similarity is often a better distance measure.

Why k-nearest neighbours breaks without feature scaling

⚠️ This is the mistake that ruins most first attempts

Two features: age (18–80) and salary (£20,000–£150,000). Two people differing by 40 years of age and £100 of salary. The distance calculation sees √(40² + 100²) — the salary term dominates completely, and age is effectively ignored.

Because k-nearest neighbours is defined by distance, any feature with a larger numeric range silently takes over. Always scale your features first. This is not optional here.

Choosing k

k is the only real decision you make, and it directly controls the bias–variance tradeoff.

Value of kBehaviourFailure mode
k = 1Copies the single nearest pointOverfits — one mislabelled point creates a wrong pocket
k ≈ √nA sensible starting guessTune properly with cross-validation
k = nEvery point votesUnderfits — always predicts the most common class

Two practical tips: use an odd k for two-class problems so votes cannot tie, and choose k with cross-validation rather than by eye.

The unusual thing about training

✅ k-nearest neighbours is a “lazy learner”: training does nothing but store the data. All the work happens at prediction time, when it must measure the distance to every stored point. This is the exact reverse of most models, which are slow to train and instant to predict.

The consequence is a real one. With 100,000 training rows, every single prediction requires 100,000 distance calculations. k-nearest neighbours is therefore a poor choice when predictions must be fast or the dataset is large — though structures like KD-trees and ball trees speed it up considerably in low dimensions.

The curse of dimensionality

k-nearest neighbours degrades badly as you add features, for a reason that is genuinely counter-intuitive: in high dimensions, everything is far from everything else, and roughly equally far.

🧪 Why distance stops meaning anything

Scatter 100 random points in a 1-D line from 0 to 1 and your nearest neighbour is typically about 0.01 away — genuinely close.

Scatter the same 100 points in 100 dimensions and the nearest and furthest points end up at almost the same distance from you. “Nearest” stops carrying information, and a vote among neighbours becomes a vote among effectively random points.

Practical rule: with more than about 20 features, either reduce dimensions with PCA first, or use a different algorithm.

When to reach for it

Use k-nearest neighbours when your dataset is small to medium, you have few features, the decision boundary is irregular, and you want a strong baseline in five minutes. Recommendation systems built on “people similar to you” are k-nearest neighbours in spirit.

Avoid it for large datasets, high-dimensional data, or anywhere prediction latency matters.

🔑 Key Takeaways

  • k-nearest neighbours predicts by voting among the k closest training points.
  • There is no training — the model just stores the data, so prediction is the slow part.
  • Feature scaling is mandatory, because the algorithm is built entirely on distance.
  • Small k overfits, large k underfits; pick k with cross-validation and keep it odd for two classes.
  • It degrades badly beyond roughly 20 features — the curse of dimensionality.

Further reading

scikit-learn’s nearest neighbours guide documents the distance metrics and the tree structures used to speed up search, and Wikipedia on the curse of dimensionality gives the mathematics behind the effect described above.

Where to go next

© 2026 mlforbeginners.com. Educational content, free to use.
Scroll to Top