TL;DR
The nearest-class-mean classifier represents each class by the average of its examples, which is a single point called its prototype. To label a new input, it finds the prototype closest to that input and returns that class. It is one of the simplest supervised classifiers, and because each class needs only its average, new classes can be added later without rebuilding everything.
Builds on: Supervised Learning, Euclidean Distance · Leads to: iCaRL: Incremental Classifier and Representation Learning
Where it's used
Learning new classes over time. A system that must learn new categories as they arrive cannot keep every old training example forever. The iCaRL method (Rebuffi and colleagues, arXiv 2016, published at CVPR 2017) classifies with exactly this rule. It keeps a small set of stored examples per class, called exemplars, takes their average feature vector as each class prototype, and assigns each image to the nearest one. The total number of stored exemplars is capped, so each class gets fewer as more classes arrive.
Cheap addition of categories. Mensink and colleagues studied this rule for large image collections, under titles about generalizing to new classes at near-zero cost: a new class needs only its own average, computed from a stream of examples. The iCaRL paper says the rule has been shown to work well and to be more robust than standard parametric classifiers in an incremental setting. The lesson for a reader is the mechanism, not a benchmark number.
A baseline with a fixed feature extractor. Many systems first turn each input into a list of numbers with a model that is already trained, then do the classification on those numbers. If the list of numbers already separates the classes well, a class average is often enough to make a good decision. The classifier is only as good as the features it is given.
How it works
Think of each input as a point on a plane. Here are two classes, A and B, with two training points each.
- Class A: (1, 1) and (3, 1)
- Class B: (1, 5) and (3, 5)
A new point (2, 2) arrives. Which class should it get?
- Start with the examples of each class. Each input has already been turned into a point with two coordinates. This point is its feature vector.
- Average the points of each class, coordinate by coordinate (the mean of each coordinate). For A, the first coordinates are 1 and 3, averaging to 2; the second coordinates are 1 and 1, averaging to 1. So the prototype of A is (2, 1). For B the prototype is (2, 5). Check: each prototype lies exactly halfway between its two examples.
- Keep only the two prototypes. The training points themselves are not needed for classification. Two points are stored, one per class.
- Measure the distance from the new point to each prototype. From (2, 2) to (2, 1) the straight-line distance is 1. From (2, 2) to (2, 5) it is 3. Check: the horizontal parts are both 0, so the distances come only from the vertical parts, 1 and 3.
- Choose the closest prototype. 1 is less than 3, so the new point gets class A.
flowchart LR T[Labelled examples] --> M[Average each class] M --> P[One prototype per class] N[New input] --> D[Distance to every prototype] P --> D D --> L[Label of the closest prototype]
The math
Given labelled feature vectors with labels , the prototype of class is the average of the feature vectors in that class. Let be the number of examples of class . A new input receives the label of the nearest prototype under Euclidean distance.
Step 2 in symbols: the prototype is an average
For class , write for the set of indices with . Then
For the example, contains (1, 1) and (3, 1), so and .
Step 4 in symbols: the distance and the decision
The Euclidean distance from to a prototype is . The label is
Here means "the class that makes the expression smallest". For the new point , and . The smaller is 1, so .
Adding examples without storing them
The average can be updated one example at a time. If class already has examples with mean , and a new example arrives, the new mean is
Check with class A: start from one example (1, 1), so and . Add (3, 1). Then , which matches Step 2.
Where the boundary lies
Two prototypes give a boundary where the two distances are equal. Setting and expanding gives
This is a straight line in two dimensions, or a flat plane in more. So the nearest-class-mean classifier is a Linear Classifier whose boundary sits halfway between the two prototypes, perpendicular to the line joining them. Check with the example: and the right side is . So and , which is the midpoint between the prototypes at heights 1 and 5.
Worked examples
Class A has examples 2 and 4. Class B has examples 10 and 12.
- The prototype of A is . The prototype of B is .
- New input . Its distance to 3 is . Its distance to 11 is .
- The smaller distance is 3, so label as A.
- New input . Its distance to 3 is 5. Its distance to 11 is 3.
- Now the smaller distance is 3 to B, so label as B.
Interpretation: the boundary is the midpoint, . Inputs below 7 go to A and inputs above 7 go to B.
Use the classes from the walkthrough, with prototypes and .
- New input . Its squared distance to A is .
- Its squared distance to B is .
- Since , the nearest prototype is B, so label as B.
- Check the boundary from the math section: the line is . The point has , so it lies on the B side. The two results agree.
- Now take . Its squared distance to A is , and to B it is . Since , label it A. Here , on the A side.
Interpretation: with prototypes stacked vertically, the decision depends only on which half of the plane the point is in. The first coordinate does not matter here, because both prototypes share it.
An image system knows two classes, A (prototype ) and B (prototype ). Three labelled examples of a new class, bird, arrive: , and . The model is not retrained. The new prototype is their average, and the old prototypes are left as they are.
- Bird prototype: first coordinates ; second coordinates . So the prototype is .
- A new input arrives. Its squared distance to A is .
- Its squared distance to B is .
- Its squared distance to bird is .
- The smallest is 0.25, so label as bird.
Try it: the same three prototypes, A at , B at and bird at . Classify using squared distances. Give the label.
Answer: Squared distance to A: . Squared distance to B: . Squared distance to bird: . The smallest is 1.25, so the label is B. Squared distances give the same ranking as distances, so the square root is not needed.
Common mistakes and tradeoffs
It is tempting to think the nearest-class-mean classifier is the same as the k-nearest neighbors method. In fact the two differ in what they store. The nearest-neighbor method keeps every training example and votes among the closest ones. The nearest-class-mean classifier keeps one point per class. That is why it needs little memory and suits incremental learning, where the paper's definition forbids storing all past training data.
It is tempting to think the average is always the best representative of a class. In fact the average can land in an empty region when a class has two separate clusters. Suppose class A has examples near (0, 0) and near (10, 0). Its mean is (5, 0), which is far from both clusters. Now let class B have mean (4, 0). The point (0, 0) sits on top of an A example, but it is 4 away from B's mean and 5 away from A's, so the rule labels it B. Examples that are spread out or lumpy are the typical failure.
It is tempting to think the classifier learns the features. In fact it uses whatever features it is given. The iCaRL paper notes that the nearest-class-mean rule is hard to combine with learning a nonlinear representation at the same time, because the means then cannot be updated incrementally. iCaRL therefore uses averages over a selected set of exemplars and learns the features with a separate training procedure.
Use the nearest-class-mean rule when the features already separate the classes well, when classes arrive over time, and when memory or compute is tight. Do not use it when classes are made of several distinct clusters, when the distance between raw inputs is not meaningful, or when a trained linear classifier would do better on the same features. Alternatives include the nearest-neighbor method, which handles clusters but stores all data, and a trained linear classifier, which learns its own boundary.
History
Assigning a new item to the closest class average is an old idea in pattern recognition; the simplest form is sometimes called the nearest centroid rule. Mensink and colleagues brought it to large image collections in papers from 2012 and 2013, arguing that it could take on new classes at near-zero cost. The iCaRL method, published by Rebuffi and colleagues in 2016 as an arXiv preprint and at CVPR in 2017, put the same idea inside a training procedure for classes that arrive over time. Its appeal was simple: it works without keeping all past data, and its only extra cost is the average.
Recap
- Each class is represented by one prototype: the average of its feature vectors.
- A new input gets the label of the nearest prototype under Euclidean distance.
- Prototypes can be updated one example at a time, so new classes need only their own average.
- The decision boundary is a flat line or plane halfway between prototypes, so the rule is a linear classifier.
Read next: iCaRL: Incremental Classifier and Representation Learning applies this rule in a continual learning setting, which is covered in Continual Learning.
Check yourself
QuestionWhat is stored for each class in a nearest-class-mean classifier, and how is it computed?Show answer
QuestionClass A has examples (0, 0) and (2, 0), so its prototype is (1, 0). Class B has examples (0, 4) and (2, 4), so its prototype is (1, 4). Which label does the rule give to the input (1, 3)?Show answer
QuestionWhy can a nearest-class-mean classifier add a new class without retraining on the old data?Show answer
QuestionA class has two separate clusters of points, far apart. Why might the nearest-class-mean rule misclassify inputs near one cluster, and what would you change?Show answer
Sources
- Rebuffi, Kolesnikov, Sperl and Lampert. iCaRL: Incremental Classifier and Representation Learning. https://arxiv.org/abs/1611.07725 (DOI: https://doi.org/10.1109/CVPR.2017.587)
- K-nearest neighbors algorithm. Wikipedia. https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm