What is k-nearest neighbors (k-NN)?
k-nearest neighbors is a method that predicts from the k training points closest to a query. For classification it takes a majority vote of their labels, and for regression it averages their target values. There is no training step beyond storing the data, so all of the work happens at prediction time.
k-NN is a standard baseline, and the same nearest-neighbor search appears in recommendation ("users similar to you"), anomaly detection, missing-value imputation, and the vector search behind retrieval-augmented generation, where a query embedding is matched to its nearest document embeddings. Its results depend on the value of k, the distance metric, and whether the features are on comparable scales.
Key ideas
- k-NN predicts from the labels of the k training points closest to the query.
- It stores the training data and does all of its computation at prediction time.
- Small k follows local detail and noise, while large k gives smoother, more biased boundaries.
- The distance metric and the feature scaling decide which points count as neighbors.
- Distance weighting gives closer neighbors stronger votes.
- In high dimensions distances concentrate, and plain k-NN loses its discriminating power.
Common questions
Is k-NN supervised or unsupervised?
k-NN classification and regression are supervised, because they use labeled training points. It is often confused with k-means, an unsupervised clustering method. The k in k-means is the number of clusters, not the number of neighbors.
Why do features need to be scaled for k-NN?
A distance adds up differences across all features, so a feature measured in thousands swamps one measured in fractions. Standardizing each feature to zero mean and unit variance, or rescaling it to [0, 1], gives every feature a comparable say in who counts as a neighbor.
How does k-NN scale to large datasets?
A brute-force search compares the query with every training point, which costs O(n·d) per prediction. Tree indexes such as k-d trees and ball trees speed this up in low dimensions. For millions of high-dimensional vectors, approximate nearest-neighbor indexes such as HNSW trade a small loss in accuracy for large speedups.
Can k-NN be used for regression?
Yes. k-NN regression predicts the average target of the k nearest neighbors, optionally weighted by distance. The prediction is piecewise constant and cannot extrapolate beyond the range of targets seen in training.
Related explainers
- Norms & Distance Metrics — L1, L2, cosine similarity — measuring closeness in vector spaces
- Curse of Dimensionality — Why high-dimensional spaces break intuition and algorithms
- Feature Scaling — Why and how to normalize features for faster convergence
- K-Means Clustering — Unsupervised grouping by centroid distance
- Decision Trees & Splitting — Recursive partitioning, Gini impurity, and pruning
- Bias-Variance Tradeoff — Why models underfit or overfit — the fundamental decomposition
References
- Cover & Hart (1967), "Nearest Neighbor Pattern Classification", IEEE Transactions on Information Theory
- Beyer, Goldstein, Ramakrishnan & Shaft (1999), "When Is 'Nearest Neighbor' Meaningful?"
- James, Witten, Hastie & Tibshirani, "An Introduction to Statistical Learning", sections 2.2 and 3.5
Last updated