Skip to content
Kudos AI

k-Nearest Neighbours

A nonparametric classifier that predicts the class of a point by taking a majority vote among the k training observations closest to it.

Also known as: KNN

Understanding k-Nearest Neighbours

Given a point to classify, k-nearest-neighbours finds the k training observations nearest to it, estimates the class probabilities as the proportions among those neighbours, and predicts the most common one. Nothing is fitted in advance: the entire training set is retained and consulted at prediction time, which makes training instantaneous and prediction expensive - the reverse of most methods.

The choice of k controls flexibility directly. At k = 1 every training point is its own nearest neighbour, so training error is exactly zero and the boundary is jagged: a high-variance fit that responds to individual observations. As k grows the boundary smooths, variance falls and bias rises, until at k approaching the sample size almost the whole training set votes on every prediction and the classifier returns the majority class regardless of input.

Because it imposes no shape on the boundary, the method can approximate curved and disconnected decision regions that a linear model cannot represent at all. The price is that it has no way to ignore an irrelevant feature: every dimension contributes to the distance, so accuracy degrades as uninformative features are added - a form of the curse of dimensionality, in which the nearest neighbours in high dimensions are not close in any useful sense.

The distance metric deserves attention that it usually does not get. Euclidean distance on raw features lets a variable measured in large units dominate the calculation, so features should be standardised unless their relative scales are deliberately meaningful. The method also offers no coefficients and no summary: it can say what it predicts but not why, which rules it out where the reasoning has to be inspectable.

How to Calculate

P(Y = j | X = x₀) = (1/k) Σ_{i ∈ N₀} I(yᵢ = j)

where

x₀
the point being classified
N₀
the k training observations nearest to x₀
I(yᵢ = j)
one when neighbour i belongs to class j, zero otherwise
k
the number of neighbours consulted - the flexibility parameter

Example of k-Nearest Neighbours

On a two-dimensional problem whose optimal error rate is 0.092708, k-nearest-neighbours fitted to 200 training points and scored on 20,000 test points gives an error of 0.138200 at k = 1, falls to a best value of 0.097150 at k = 15, and rises again to 0.103550 at k = 75.

At k = 199, out of 200 training points, the error reaches 0.504850 - essentially the class prior, because almost the entire sample votes on every prediction. At the other end, k = 1 has a training error of exactly zero while its test error is the worst of every k but the degenerate k = 199, which is the clearest possible demonstration that training error is not an estimate of test error.

The best value is interior, and there is no formula for it. It is chosen by cross-validation, and the shallow floor between k = 9 and k = 45 here is typical: the method is not usually sensitive to getting k exactly right, only to getting it roughly right.

Frequently Asked Questions

How is k chosen?

By cross-validation, essentially always. The test-error curve is U-shaped in k, so the search is well behaved, and an odd k avoids ties in two-class problems. There is no analytical answer because the optimum depends on the noise level and the local density of the data.

Why does performance fall as features are added?

In high dimensions the training points are all far apart and roughly equidistant, so the k nearest neighbours of a test point are not local to it in any meaningful sense. The method then averages over observations that carry little information about the point being predicted.

Is it ever preferable to a parametric method?

Yes, when the true decision boundary is strongly non-linear and there is enough data to trace it. When the boundary really is close to linear, a linear method will beat it, because the parametric assumption is a genuine saving rather than a restriction.

The Bottom Line

k-nearest-neighbours makes no assumption about the shape of the boundary and can therefore find shapes no linear model can express, at the cost of needing more data, more prediction time and careful feature scaling. Standardise, choose k by cross-validation, and expect it to fade as the number of irrelevant features grows.