K-Nearest Neighbors (k-NN) is a non-parametric supervised learning algorithm used for both classification and regression. In classification, a new data point is assigned the class most common among its k nearest neighbors in the feature space, determined by a distance metric. In regression, the output is the average (or weighted average) of the values of those neighbors. The algorithm is instance-based, meaning it stores the entire training dataset and performs computations only when a prediction is required, deferring all generalization until query time.
The method was first developed by Evelyn Fix and Joseph Hodges in 1951 and later expanded by Thomas Cover. It is one of the simplest machine learning algorithms, yet it can achieve competitive accuracy in many domains, especially when the decision boundary is irregular. Its performance heavily depends on the choice of k, the distance metric, and feature scaling.
Historical Development
The origins of k-NN trace back to 1951 when Evelyn Fix and Joseph Hodges, working at the US Air Force School of Aviation Medicine, introduced a non-parametric classification method based on nearest neighbors. Their work was motivated by the need to classify observations without assuming a specific statistical distribution. In 1967, Thomas Cover and Peter Hart published a seminal paper that formalized the algorithm's properties, including bounds on its error rate relative to the Bayes optimal classifier. This established k-NN as a theoretically grounded approach in pattern recognition. The algorithm gained popularity in the 1960s and 1970s with the rise of computing, as it required minimal training time but substantial storage. Later developments, such as the introduction of weighted voting and distance metric learning, addressed some of its limitations.
Algorithm Overview
In k-NN classification, the input consists of a training set of labeled examples, each represented as a feature vector in a multidimensional space. The algorithm stores these vectors and their labels. When a query point is presented, it computes the distance from the query to all training points, selects the k closest ones, and assigns the class that appears most frequently among them. For k=1, the query is simply assigned to the class of its nearest neighbor. The choice of k is critical: a small k can lead to high variance and sensitivity to noise, while a large k may oversmooth the decision boundary and include points from other classes.
For regression, the output is the average of the target values of the k nearest neighbors. This is known as nearest neighbor smoothing. If k=1, it becomes nearest neighbor interpolation, where the predicted value is exactly that of the closest training point. Weighted variants assign higher influence to closer neighbors, often using weights proportional to the inverse of the distance (1/d).
Distance Metrics and Feature Scaling
The choice of distance metric is crucial. For continuous features, Euclidean distance is most common. For discrete features, such as in text classification, Hamming distance or overlap metrics are used. In specialized domains like gene expression analysis, correlation coefficients (Pearson, Spearman) have been employed. The algorithm's reliance on distance means that features with different units or scales can dominate the calculation. Therefore, normalizing each feature to a common scale (e.g., z-score or min-max scaling) is essential to ensure equal contribution. This preprocessing step can significantly improve accuracy.
Statistical Properties
From a statistical perspective, k-NN is a non-parametric method because it does not assume a functional form for the underlying data distribution. The training data are assumed to be pairs (X_i, Y_i) where X_i is a feature vector and Y_i is the class label. For a given query point x, the training points are reordered by their distance to x. The algorithm's error rate converges to the Bayes error rate as the sample size increases, provided k grows appropriately with n and k/n approaches zero. This property, established by Cover and Hart, makes k-NN asymptotically optimal. However, in finite samples, the algorithm suffers from the curse of dimensionality: as the number of features increases, the volume of space grows exponentially, and points become sparse, making distance measures less meaningful.
Advantages and Disadvantages
One major advantage of k-NN is its simplicity and lack of a training phase. It can be updated easily by adding new data points. It is also effective for multi-class problems and can capture complex decision boundaries. However, it has notable disadvantages. Prediction time is slow because it requires computing distances to all training points, making it impractical for large datasets without optimization (e.g., using KD-trees or ball trees). It is sensitive to irrelevant features and noisy data. The algorithm is also sensitive to the local structure of the data, meaning that outliers or imbalanced class distributions can skew results. In skewed distributions, majority classes dominate because they are more likely to appear among the k neighbors. Weighting by inverse distance or using abstraction techniques can mitigate this.
Variants and Extensions
Several variants address k-NN's limitations. Weighted k-NN assigns weights to neighbors based on distance, so closer points have more influence. Distance metric learning methods, such as large margin nearest neighbor and neighborhood components analysis, learn a custom distance metric to improve accuracy. Edited k-NN removes noisy or misclassified training points to improve generalization. Condensed k-NN reduces the training set size by keeping only points that are essential for classification. Locally adaptive k-NN adjusts k based on the density of the region around the query point. These variants have been applied in fields like Machine learning and Artificial intelligence to improve performance.
Applications
The k-NN algorithm is used in various domains. In pattern recognition, it is applied to image classification and handwriting recognition. In medicine, it is used for diagnosis based on patient features. In finance, it helps in credit scoring and fraud detection. In recommendation systems, it finds similar users or items. In bioinformatics, it classifies gene expression data. Its simplicity makes it a common baseline for comparing more complex models like Neural network and Deep learning.
Relationship to Other Methods
k-NN is a form of instance-based learning, distinct from model-based approaches like Neural network or Support Vector Machine that build an explicit model during training. It is also related to non-parametric density estimation. In the broader context of Machine learning, k-NN is often used as a benchmark. It has influenced the development of locality-sensitive hashing and approximate nearest neighbor search, which are used in large-scale systems. While modern methods like Deep learning have surpassed k-NN in many tasks, k-NN remains valuable for small datasets and interpretable predictions.
Practical Considerations
When implementing k-NN, several practical issues arise. The value of k is typically chosen via cross-validation. Odd values of k are often used to avoid ties in binary classification. Feature scaling is essential. Efficient data structures like KD-trees can accelerate nearest neighbor search, but they degrade in high dimensions. For very large datasets, approximate methods are necessary. The algorithm's memory usage is proportional to the training set size, which can be a limitation. In modern applications, k-NN is sometimes combined with other algorithms, such as using it as a final classifier on top of learned embeddings from a Neural network.
See Also
- Machine learning
- Artificial intelligence
- Neural network
- Deep learning
- Stanford AI Lab
- MIT CSAIL
- Carnegie Mellon University
- BAIR (Berkeley AI Research)
- Xerox PARC
- Thomas G. Dietterich
- Michael I. Jordan
- Daphne Koller
- Anima Anandkumar
- Samy Bengio
- Joshua Tenenbaum
- Brendan Lake
- Melanie Mitchell
- Aaron Courville
- Alexei Efros
- Ali Rahimi