K-nearest neighbours (k-NN)
K-nearest neighbours is a simple machine learning method that labels or estimates a value for a new data point by looking at the k most similar examples in its stored training data.
K-nearest neighbours barely trains at all. It stores every labelled example. When a new point arrives, it measures the distance from that point to each stored example, picks the k closest, and lets them decide: for classification the most common label among the neighbours wins, and for regression the prediction is their average, sometimes weighted so that nearer neighbours count more. With k set to 1, a new point simply takes the label of its single closest match.
It works much like an estate agent valuing a house by looking at comparable sales nearby. If the five most similar homes sold for around a certain price, that is a reasonable estimate for this one. The method makes no assumptions about the shape of the data, which is why it is described as non-parametric, and scikit-learn's documentation calls it non-generalising: it does not build a model, it keeps the examples.
The method was developed by Evelyn Fix and Joseph Hodges in 1951 and later expanded by Thomas Cover. One classic result explains its appeal: as the amount of data grows, the error rate of the single nearest neighbour rule on a two-class problem is at most twice the lowest error rate any classifier could achieve.
The choices that matter are k and the distance measure. A larger k smooths out noise but blurs the boundaries between classes, and an odd k avoids tied votes between two classes. Euclidean distance suits continuous measurements, and features must be put on comparable scales or the one with the largest numbers will dominate. All the work happens at prediction time, so a naive search compares every new point with every stored example. Tree-based indexes help in low dimensions, but scikit-learn notes they become inefficient as the number of dimensions grows, and in high dimensions distances themselves become less informative, a problem known as the curse of dimensionality.
The same idea sits under much of modern AI search. When a retrieval system looks up the documents whose embeddings are closest to a question, it is doing a nearest neighbour search, usually an approximate one that trades a little accuracy for enormous speed gains over millions of vectors.
It stores all the training examples. To make a prediction for a new point, it finds the k stored examples closest to it and takes a majority vote of their labels for classification, or the average of their values for regression. The best k depends on the data and is usually found by testing several values on held-out data. Small values follow the training data closely and are sensitive to noise, while large values smooth out noise but blur class boundaries. For two-class problems an odd k avoids ties. K-nearest neighbours for classification and regression is supervised, because it needs labelled examples. The nearest neighbour search underneath it can also be used without labels, for example to find similar documents. They only share a letter. K-nearest neighbours is a supervised method that predicts labels from the k closest labelled examples. K-means is an unsupervised clustering method that splits unlabelled data into k groups around central points.Common questions
How does k-nearest neighbours work?
How do you choose k in k-NN?
Is k-NN supervised or unsupervised?
What is the difference between k-NN and k-means?
Search for "k-nearest neighbours classifier", "choosing k in kNN" or "approximate nearest neighbour search" to see both the classic method and its role in modern retrieval. Reliable sources include the documentation of machine learning libraries such as scikit-learn, statistics and machine learning textbooks, and encyclopedia articles that cite the original work. Note that American spelling, k-nearest neighbors, is more common in code and documentation.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.