AppsGuidesAI DictionarySmall FixesOn RecordAboutFollow the lab

k-nearest neighbours

Also called knn, k nearest neighbors

K-nearest neighbours labels something new by finding the few past examples most like it and going with whatever most of them were.

In plain words

Imagine you have a big pile of examples that are already sorted, like past customers marked "bought" or "did not buy". Now a new one arrives. K-nearest neighbours, or KNN, simply finds the examples most similar to it and looks at their answers.

The "k" is how many neighbours it asks. With k set to 5, it picks the five closest examples and takes a majority vote. If three of the five bought, it guesses the new customer will buy too. For a number, such as a price, it takes the average of the neighbours instead.

There is almost no training. KNN just keeps every example and does all its work at the moment you ask, which makes it easy to understand but slow when the pile gets huge. It is a classic method for classification and a simple kind of supervised learning.

Everyday example

Asking the neighbours about internet

You move into a new building and want to know which internet provider works best on your floor. You ask the five nearest flats. Three say one provider, two say another. You go with the one most neighbours chose. That is KNN with k set to 5: nearness picks who you ask, and a vote decides the answer.

Where the example breaks: Your neighbours are near on a map. In KNN, "near" means similar in the details you chose to measure, so if you measure the wrong things, or one measurement drowns out the rest, the closest examples can be poor guides.

Go deeper

How it actually works

KNN is called a lazy or instance-based method: it builds no model in advance, it stores the training examples and compares against them at prediction time.

  1. Every example becomes a point

    Each example is described by the same set of features, so it can be placed as a point in a space with one direction per feature. A customer might be age, visits per month and average bill. Similar examples sit close together.

  2. Measure distance

    For a new point, KNN measures its distance to every stored point. Straight-line (Euclidean) distance is the most common choice; others include Manhattan and cosine distance. Because distance mixes all features, people usually rescale features to similar ranges first, or one large-numbered feature dominates.

  3. Pick k, then vote or average

    The k closest points decide. For classification it is a majority vote; for regression it is the mean of their values. Closer neighbours can be given more weight. A small k follows noise, while a large k smooths the boundary but can blur real detail, so k is a hyperparameter, often chosen by testing several values.

  4. The cost moves to prediction time

    Because nothing is learned up front, each prediction compares against the stored data. With many examples or many features this gets slow, and it copes poorly with very many features, a problem called the curse of dimensionality. Indexes such as KD trees speed up searches in low dimensions.

First described
A 1951 technical report by Evelyn Fix and Joseph Hodges, only published in full in 1989
Key result
Cover and Hart (1967) showed that with enough data, the 1-neighbour rule's error is at most twice the lowest error possible
Type of learning
Supervised, for categories or numbers
Used for
Simple classifiers, recommendations, data preparation, pattern recognition

The same "find the closest items" search sits behind vector databases and semantic search, where the points are embeddings of text or images.

Questions people ask

What does the k in KNN mean?

The number of neighbours that get a say. With k = 1 the single closest example decides. With k = 7, the seven closest vote. People often pick an odd k for two-class problems so a vote cannot tie.

Is KNN supervised or unsupervised?

Supervised. It needs examples whose answers are already known, because the neighbours' answers are what it copies. It is often confused with k-means, a clustering method that groups unlabelled data and is unsupervised.

What is the difference between KNN and k-means?

KNN predicts an answer for a new item from labelled neighbours. K-means sorts unlabelled items into k groups by moving k centre points until each group is tight. Both use distance, and both have a k, but they solve different problems.

How do you choose the value of k?

By trying several values and checking which predicts best on held-out data, often with cross-validation. Too small and the answer swings with every noisy example; too large and it starts ignoring local detail.

Why is KNN slow on big datasets?

Because it does its work at question time. Every prediction compares the new item with the stored examples, so millions of examples mean millions of distance checks unless a search index is used.

Was this clear?

1 is confusing, 5 is crystal clear. One tap, no account needed.

Spotted something wrong, or know a better example? Suggest a change

Further reading