Classical machine learningML 04

k-Nearest Neighbours

The simplest way to classify something new is to look at the most similar cases already seen and go with the majority. k-nearest neighbours does exactly that: to classify a point it finds the k training points closest to it and lets them vote. There is no training step at all; the data are the model. The same idea underlies recommendations (“people who bought this also bought…”) and finding similar patients or documents. Hover over the plot to see which neighbours vote, and click to add patients. The data are fictional and simplified.

feature spacelive
click: add point · right-click: remove · hover: see the vote

Accuracy for every k vertical line: the current k

shading: the class that would win the vote at each spot, stronger where the vote is more one-sided · lines: the k nearest neighbours of the pointer · dashed outline: how far the neighbourhood reaches · hollow: test points

Choosing k

With k = 1 every training point decides its own small territory, so the boundary is jagged and follows every quirk and mislabelled case: training accuracy is a perfect 100% because each point is its own nearest neighbour (unless two points with different labels sit in exactly the same place). Larger k smooths the boundary by averaging over more cases, until with k close to the number of points the majority class wins everywhere. Training accuracy is therefore no guide. The chart also shows leave-one-out accuracy, where each training point is classified by the others with itself left out, and accuracy on a held-out test set: both usually peak at a moderate k. Weighting votes by 1 / distance lets the closest cases count for more.

Distances need comparable units

“Nearest” depends on how distance is measured. Glucose varies over a few mmol/L while BMI varies over tens of kg/m², so in raw units a difference of 1 in either counts the same, and BMI dominates: on the diabetes data the dashed neighbourhood is a wide, flat ellipse on the plot, stretching across most of the glucose range while covering only a narrow band of BMI. Turning on feature scaling standardises each feature (subtracts its mean and divides by its standard deviation) so that both count equally, and the neighbourhood becomes round once each axis is measured in standard deviations. Which choice is right is a judgment about the problem, but it should always be a deliberate one.

Strengths and limits

Fix and Hodges described the method in 1951 in a report for the US Air Force School of Aviation Medicine; Cover and Hart (1967) proved that, with enough data, the error of the single nearest neighbour is at most twice the lowest error any classifier could achieve. Nearest neighbours needs no assumptions about the shape of the boundary and adapts to any number of classes, but it must keep and search the whole training set for every prediction, and in many dimensions all points end up roughly equally far apart, so “nearest” loses its meaning.

References

  1. Fix, E., & Hodges, J. L. (1951). Discriminatory analysis. Nonparametric discrimination: consistency properties. Report No. 4, Project 21-49-004. USAF School of Aviation Medicine, Randolph Field, Texas. Reprinted in International Statistical Review (1989), 57(3), 238–247, with a commentary by B. W. Silverman & M. C. Jones.
  2. Cover, T. M., & Hart, P. E. (1967). Nearest neighbor pattern classification. IEEE Transactions on Information Theory, 13(1), 21–27. doi.org/10.1109/TIT.1967.1053964
  3. Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning (2nd ed.), chapters 2.3 and 13. Springer. doi.org/10.1007/978-0-387-84858-7