Neighbourhood Components Analysis

Jacob Goldberger, Sam T. Roweis, Geoffrey E. Hinton, Ruslan SalakhutdinovNeurIPS 2004

TL;DR

Neighbourhood Components Analysis (NCA) is a powerful method for learning a distance metric that improves K-Nearest Neighbor (KNN) classification. By maximizing a "soft," differentiable version of leave-one-out performance, NCA learns a linear transformation that excels at both classification and low-dimensional data visualization without making restrictive assumptions about the shape of your data.

What Problem Does This Paper Address?

The K-Nearest Neighbor (KNN) algorithm is a classic in machine learning because it’s simple and non-parametric—it doesn't assume your data follows a specific distribution (like a Bell curve). However, KNN has two major "Achilles' heels":

  1. Metric Sensitivity: It relies entirely on a distance metric (usually Euclidean). If your features are on different scales or if some are noisy, the "nearest" neighbors might not actually be the most relevant ones.
  2. Computational Cost: You have to store and search the entire training set at test time.

While techniques like Linear Discriminant Analysis (LDA) exist to find better data projections, they often assume classes are Gaussian (normally distributed) and unimodal. If your classes are shaped like rings or are split into multiple clusters, traditional methods often fail.

Methods

The authors propose Neighbourhood Components Analysis (NCA). The core idea is to learn a linear transformation matrix $A$ such that in the transformed space, KNN performance is maximized.

Because the standard KNN error is "jumpy" (discontinuous)—a tiny change in the transformation could suddenly swap which point is the nearest neighbor—the authors introduce a stochastic ("soft") neighbor selection rule.

  • Instead of a point $i$ having a single fixed neighbor, it selects point $j$ as its neighbor with a probability $p_{ij}$ based on a softmax over their distances in the transformed space.
  • The objective is to maximize the expected number of points correctly classified under this stochastic scheme.
  • By using a non-square matrix $A$ (e.g., $d \times D$ where $d < D$), the algorithm simultaneously performs dimensionality reduction, projecting high-dimensional data into a lower-dimensional space that preserves class separability.

Datasets

The authors evaluated NCA on several well-known benchmarks:

  • UCI Machine Learning Repository: Balance, Ionosphere, Iris, Wine, and Housing datasets.
  • USPS Handwritten Digits: A subset of the first five digit classes ("1" through "5"), downsampled to $8 \times 8$ pixels.
  • Face Images: A database of grayscale faces (18 individuals, 1800 images total) and the FERET-B dataset for semi-supervised experiments.

Key Insights and Ideas

  • Differentiable KNN: By turning a discrete "hard" neighbor choice into a "soft" probability, the authors made the KNN objective differentiable. This allows the use of standard gradient-based optimizers (like conjugate gradients).
  • No Parametric Assumptions: Unlike LDA, NCA doesn't care if your classes are non-convex or multimodal. It focuses on local neighborhood structure rather than global class statistics.
  • The "Overtraining" Mystery: Interestingly, the authors noted they never observed an "overtraining" effect. Driving the training objective higher consistently led to better test performance, suggesting the linear constraint acts as a strong regularizer.
  • Learning "K": By adjusting the overall scale of the matrix $A$, the algorithm effectively learns the optimal "perplexity" or neighborhood size, essentially tuning the "K" in KNN automatically.

Key Results

Performance comparison of NCA against other methods for full-rank metric learning and rank-2 transformations on training and testing sets.
  • Classification Accuracy: In almost all tested UCI datasets, NCA matched or outperformed standard Mahalanobis metrics, "whitening" transformations, and RCA (Relevant Components Analysis). This held true across both training and testing sets for full-rank and low-rank transformations.
  • Dimensionality Reduction: NCA proved highly effective at reducing search costs. For example, on the "digits" dataset, NCA maintained high accuracy even when reducing the data from 64 dimensions down to just 2.
2D projections of various datasets comparing PCA, LDA, and NCA. NCA successfully separates complex, non-Gaussian distributions like concentric rings (top row).
  • Visualization: When projecting data down to just 2 dimensions, NCA produced significantly cleaner class separation than PCA (which is unsupervised) or LDA. LDA notably struggled with non-Gaussian shapes like concentric rings, whereas NCA successfully unraveled and separated them.

Limitations and Future Work

  • Non-Convexity: The objective function is not convex, meaning the optimization can get stuck in local maxima. The authors suggest that care must be taken during training (e.g., multiple restarts).
  • Fixed Dimensionality: Currently, the user must manually choose the target dimensionality ($d$).
  • Computational Scaling: While more efficient than some methods, calculating the gradient involves sums over all pairs of points, which can be slow for very large datasets (though the authors suggest truncating these sums to speed things up).
  • Future Directions: The authors suggest exploring non-linear transformations (using neural networks) and using local estimates of $K$ based on the entropy of the neighbor distributions.

Summary updated

BibTeX
@inproceedings{goldberger2004neighbourhood,
  author = {Jacob Goldberger and Sam T. Roweis and Geoffrey E. Hinton and Ruslan Salakhutdinov},
  title = {{Neighbourhood Components Analysis}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2004},
  url = {https://openreview.net/forum?id=H1Z6t8-_bS},
}
Figure