Foundations to Frontier
Tests
D. Classical Machine Learning › D4 Instance-Based Methods

Nearest Centroid Classifier

The nearest-centroid classifier summarises each class by its mean (centroid) and assigns a new point to the class with the closest centroid. It is the simplest prototype method: instant to train, tiny to store, and surprisingly strong with few samples and many features, where k-NN and LDA struggle. Its shrunken-centroid variant (PAM) soft-thresholds standardised class deviations, which selects features automatically. This page covers the classifier, its linear boundaries and relation to LDA with identity covariance, the shrunken-centroid method and its threshold, and why centroid methods beat neighbour methods in high-dimension, low-sample problems. In the exercises you implement nearest centroid and nearest shrunken centroid; then you identify minerals from 300-bin spectra with only 5 or 20 labelled samples per class and compare centroid methods with k-NN.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

One typical example per class

Instead of remembering every labelled spectrum, remember the average spectrum of each mineral. A new spectrum is labelled by the mineral whose average it is closest to. That is the nearest-centroid classifier. Averaging cancels noise, so it works with very few examples. Its shrunken version also notices which wavelengths actually differ between minerals and ignores the rest.

Shrunken centroids: keep only the features that separate classes
★★★★★★★★
features kept 6 of 40 (truly informative kept: 6 of 8) · test accuracy 87.3% (Δ = 0: 82.7%)
Faint bars: standardised class deviations; solid: after soft-thresholding by Δ (dashed lines). Stars mark the 8 informative features. With 8 training samples per class, noise features show sizeable deviations by chance; shrinking removes them and usually improves accuracy, until Δ is so large that real signal is lost too.
Why engineers care

Spectral libraries, signature-based fault detection and template matching are nearest-centroid methods. With few labelled samples and hundreds of channels, typical in planetary science and early-mission operations, centroid classifiers are often the most accurate and most explainable choice, and shrunken centroids list the channels that matter.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A nearest-centroid classifier stores…
  2. 2. It classifies by…
  3. 3. In the demo, shrinking removes…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

Training data:

  • class 0: (0, 0), (2, 0), so centroid (1, 0);
  • class 1: (10, 10), (12, 10), so centroid (11, 10).

Classify (3, 1):

  • squared distance to (1, 0) is 4 + 1 = 5;
  • to (11, 10) it is 64 + 81 = 145;
  • so class 0.

The decision boundary is the perpendicular bisector of the two centroids: the set of points equidistant from them, which is a straight line.

Shrinking. For each feature and class, compute the class mean’s deviation from the overall mean, in units of its standard error. Shrink it toward zero by Δ: a deviation of 3 with Δ = 1 becomes 2, and −0.5 becomes 0. A feature whose deviations are 0 in every class no longer affects any decision.

In exercise 1 (200 features, 10 informative, 20 samples per class), Δ = 1 kept 38 features, including 9 of the 10 informative ones. Accuracy rose from 0.729 to 0.866.

The steps in plain English:

  1. Compute each class’s mean (and the overall mean).
  2. Plain version: classify to the closest class mean.
  3. Shrunken version: standardise each class-mean deviation, soft-threshold it by Δ, and rebuild the centroids from the shrunken deviations.
  4. Classify with the shrunken centroids, scaling each feature by its pooled standard deviation and adding a log-prior term.
  5. Choose Δ by cross-validation. Report the features that survive.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Centroid of (0, 0) and (2, 0)?
  2. 2. Soft-threshold of −0.5 by Δ = 1?
  3. 3. The plain two-class boundary is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

The methods

Nearest centroid: ŷ = argminₖ ‖x − μ̂ₖ‖². This equals LDA with Σ = σ²I and equal priors, so the boundaries are linear.

Nearest shrunken centroid (Tibshirani, Hastie, Narasimhan and Chu, 2002):

  • standardised deviations: dₖⱼ = (μ̂ₖⱼ − x̄ⱼ)/(mₖ(sⱼ + s₀)), with mₖ = √(1/nₖ − 1/n), sⱼ the pooled within-class SD, and s₀ a small offset (the median sⱼ);
  • shrinkage: d′ₖⱼ = sign(dₖⱼ)(|dₖⱼ| − Δ)₊;
  • shrunken centroids: μ′ₖⱼ = x̄ⱼ + mₖ(sⱼ + s₀)d′ₖⱼ;
  • discriminant: Σⱼ (xⱼ − μ′ₖⱼ)²/(sⱼ + s₀)² − 2 log πₖ.

This is a diagonal LDA with lasso-like shrinkage of the class effects.

from sklearn.neighbors import NearestCentroid
clf = NearestCentroid(shrink_threshold=1.0).fit(X, y)   # shrunken centroids; None for plain
clf.centroids_

Use it when there are few samples and many features (spectra, genomics, telemetry snapshots), classes are roughly compact, and you need interpretability and feature selection. Avoid it with multi-modal classes (one centroid cannot represent several clusters; use multiple prototypes or LVQ, D4.45) or strongly correlated features.

🐍 Exercise 1: nearest centroid and shrunken centroids
Implement nearest centroid and the nearest shrunken centroid (standardised deviations, soft-thresholding, shrunken centroids, discriminant with priors); check the hand example, and show on 200-feature data with 10 informative features that shrinkage improves accuracy while keeping far fewer features.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Nearest centroid equals LDA with…
  2. 2. The shrunken-centroid threshold acts like…
  3. 3. scikit-learn's NearestCentroid parameter for shrinkage is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Why centroids win with few samples

Variance. A centroid averages nₖ points, so its noise per feature has variance σ²/nₖ. k-NN uses individual noisy points, whose distances in p dimensions are dominated by the noise in all p features. With p = 300 and 5 samples per class, k-NN’s neighbours are nearly random (exercise 2: 0.47–0.50 accuracy), while centroids reach 0.62.

Bias. A single centroid is wrong for multi-modal classes and ignores covariance. That is a large bias when classes are not compact. Prototype methods with several centroids per class (k-means per class, LVQ) reduce it.

High-dimensional theory. When p ≫ n, independence-style classifiers (diagonal LDA, nearest centroid) can beat “optimal” full-covariance LDA, whose covariance cannot be estimated (Bickel and Levina, 2004). Feature selection (shrinkage) helps further when only a few features carry signal (Fan and Fan, 2008).

Interview questions.

  • Why might nearest centroid beat k-NN? With many noisy features and few samples, averaging cancels noise while k-NN’s single-point distances are swamped by it.
  • When does it fail? Classes with several clusters, or classes that differ in spread rather than mean (use QDA, D2.29).
📝 Quick check (Level 3)
0/3 answered
  1. 1. Variance of a centroid's noise per feature with nₖ samples?
  2. 2. Bickel & Levina showed that with p ≫ n…
  3. 3. A remedy for multi-modal classes is…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Minerals from few spectra

History.

  • Nearest-mean classification goes back to early pattern recognition (Sebestyen, 1962).
  • Tibshirani, Hastie, Narasimhan and Chu (2002) introduced nearest shrunken centroids (PAM, “prediction analysis for microarrays”) for cancer diagnosis from gene expression.
  • Bickel and Levina (2004) and Fan and Fan (2008) gave theory for independence rules in high dimensions.
  • Spectral-library matching in planetary science (for example, the USGS and RELAB libraries) is a centroid or template method.

Exercise 1. With 200 features (10 informative) and 20 samples per class, plain nearest centroid reaches 0.729. Shrunken centroids at Δ = 1 reach 0.866, using 38 features, including 9 of the 10 informative ones.

Exercise 2: four minerals, 300-bin spectra. Setup: only 15 bins are informative; Δ is chosen by 5-fold CV; 10 repetitions. The table shows test accuracy.

Samples per class Nearest centroid Shrunken centroid (CV Δ) 1-NN 5-NN Bins kept
5 0.624 0.761 0.474 0.502 137
20 0.835 0.929 0.549 0.637 109
  • Centroid methods dominate k-NN in this small-sample, high-dimensional setting, by 12–30 points.
  • Shrinkage adds 9–14 points by suppressing noise bins.
  • CV keeps far more bins than the 15 informative ones (109–137). With few samples, CV prefers mild shrinkage because a large Δ also removes weak but real signal. The chosen subset is a ranking aid, not a clean selection.
🐍 Exercise 2 (research): mineral identification from few spectra
Simulate 4 mineral classes as 300-bin spectra with 15 informative bins and 5 or 20 samples per class; compare nearest centroid, nearest shrunken centroid with Δ by 5-fold CV, 1-NN and 5-NN by test accuracy, and report the number of bins the shrunken centroid keeps.

Nearest centroid in AI, robotics and aerospace

  • Spectral-library matching for mineral and material identification.
  • Signature-based fault detection: compare telemetry snapshots with nominal and fault templates.
  • Few-shot learning: prototypical networks classify by the nearest class mean in an embedding space (E15).
  • Fast baselines with interpretable per-class prototypes.

References

  1. R. Tibshirani, T. Hastie, B. Narasimhan, G. Chu (2002). “Diagnosis of multiple cancer types by shrunken centroids of gene expression.” PNAS 99(10).
  2. P. J. Bickel, E. Levina (2004). “Some theory for Fisher’s linear discriminant function, ‘naive Bayes’, and some alternatives when there are many more variables than observations.” Bernoulli 10(6).
  3. J. Fan, Y. Fan (2008). “High-dimensional classification using features annealed independence rules.” Annals of Statistics 36(6).
  4. J. Snell, K. Swersky, R. Zemel (2017). “Prototypical networks for few-shot learning.” NeurIPS.
  5. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 18.2. Springer.