Abstract
The k-Nearest Neighbors (kNN) classifier is a fundamental non-parametric machine learning algorithm. However, it is well known that it suffers from the curse of dimensionality, which is why in practice one often applies a kNN classifier on top of a (pre-trained) feature transformation. From a theoretical perspective, most, if not all theoretical results aimed at understanding the kNN classifier are derived for the raw feature space. This leads to an emerging gap between our theoretical understanding of kNN and its practical applications. In this paper, we take a first step towards bridging this gap. We provide a novel analysis on the convergence rates of a kNN classifier over transformed features. This analysis requires in-depth understanding of the properties that connect both the transformed space and the raw feature space. More precisely, we build our convergence bound upon two key properties of the transformed space: (1) safety -- how well can one recover the raw posterior from the transformed space, and (2) smoothness -- how complex this recovery function is. Based on our result, we are able to explain why some (pre-trained) feature transformations are better suited for a kNN classifier than other. We empirically validate that both properties have an impact on the kNN convergence on 30 feature transformations with 6 benchmark datasets spanning from the vision to the text domain.
Introduction
The k-Nearest Neighbor (kNN) algorithm is a classic of machine learning, prized for its simplicity and lack of a "training" phase. However, it famously falls victim to the curse of dimensionality: in high-dimensional raw feature spaces (like raw pixels), "neighbors" aren't actually very similar, and the algorithm's error rate converges painfully slowly as you add more data.
To fix this, practitioners almost always apply kNN to feature transformations (embeddings) rather than raw data. While this works great in practice, most existing kNN theory only applies to raw features. This paper provides a theoretical framework to answer: What makes a specific embedding "good" for kNN?
Theoretical Framework: Safety and Smoothness
The authors provide a new theoretical analysis of kNN convergence rates over transformed features. They decompose the error of a kNN classifier into two parts: the convergence rate of vanilla kNN in the transformed space and the inherent bias introduced by the transformation.
To make this actionable, they define two core concepts:
- Safety: Measured by the increase in the Bayes Error Rate (the minimum possible error) when moving from the raw space to the transformed space. It represents how much "useful" information was lost.
- Smoothness: Related to the complexity of a "recovery function" $g$ that maps transformed features back to original class probabilities.
The centerpiece is Theorem 4.1, which shows a trade-off: a transformation is effective if it preserves the posterior probability (Safety) and does so through a simple, non-complex function (Smoothness).
Empirical Validation
The researchers validated their theory using 30 different feature transformations across 6 benchmark datasets:
- Computer Vision: MNIST, CIFAR-10, and CIFAR-100.
- Natural Language Processing (NLP): IMDB, SST-2, and YELP.
- Transformations: Simple linear methods (PCA, NCA) and deep learning models (ResNet, VGG, EfficientNet, BERT, ELMo, USE).
Key Insights
A common myth is that kNN works better on embeddings simply because they have fewer dimensions. The authors show that transformations with the same dimensionality can have wildly different kNN accuracies.
- The Safety/Smoothness Trade-off: A transformation might be very "safe" but extremely complex (low smoothness), making it hard for kNN to find meaningful neighbors.
- Logistic Regression as a Proxy: In practice, "smoothness" can be estimated by the norm of the weights in a Logistic Regression (LR) model.
- Why Deep Embeddings Win: Pre-trained models like ResNet or BERT "linearize" the data, providing a space where the relationship between features and labels is both safe and smooth.
Key Results
- High Correlation with MSE: On vision tasks, there is a high linear correlation between 1NN error and the Mean Squared Error (MSE) of a linear classifier.
- The Power of the Norm: On text datasets, combining MSE with the norm of the weights (smoothness) using Canonical Correlation Analysis (CCA) significantly improved predictive power (e.g., from 0.84 to 0.93 on YELP).
- Predictive Theory: Theoretical bounds correctly predicted that for two transformations with similar MSE, the smoother one yields a better kNN classifier.
Conclusion and Future Work
This work bridges the gap between kNN theory and modern practice. While focused on binary classification and linear recovery functions, it provides a rigorous foundation for understanding why embeddings work. Future work includes extending the math to multi-class problems and exploring non-linear recovery functions.
Summary updated