Search

Search papers and lessons

Nearest-Class-Mean Classifier

Level 3 · 11 min read

The nearest-class-mean classifier stores one average point per class and labels a new input with the class whose average is closest. It is simple, and it can add classes without retraining.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Linear algebra
  • Probability
  • Learning basics
  • Losses & regularization
  • ReadReady to readNot yet

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?

  1. 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.
  2. 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.
  3. Keep only the two prototypes. The training points themselves are not needed for classification. Two points are stored, one per class.
  4. 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.
  5. 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

Definition: Nearest-class-mean classifier

Given labelled feature vectors xi\mathbf{x}_i with labels yiy_i, the prototype of class cc is the average of the feature vectors in that class. Let ncn_c be the number of examples of class cc. A new input x\mathbf{x} receives the label of the nearest prototype under Euclidean distance.

Step 2 in symbols: the prototype is an average

For class cc, write Sc\mathcal{S}_c for the set of indices ii with yi=cy_i = c. Then

μc=1nc∑i∈Scxi,nc=∣Sc∣.\boldsymbol{\mu}_c = \frac{1}{n_c} \sum_{i \in \mathcal{S}_c} \mathbf{x}_i, \qquad n_c = |\mathcal{S}_c|.

For the example, SA\mathcal{S}_A contains (1, 1) and (3, 1), so nA=2n_A = 2 and μA=12((1,1)+(3,1))=(2,1)\boldsymbol{\mu}_A = \tfrac12 \big((1,1) + (3,1)\big) = (2, 1).

Step 4 in symbols: the distance and the decision

The Euclidean distance from x\mathbf{x} to a prototype is ∥x−μc∥\|\mathbf{x} - \boldsymbol{\mu}_c\|. The label is

y^=arg⁡min⁡c ∥x−μc∥.\hat{y} = \arg\min_{c} \, \|\mathbf{x} - \boldsymbol{\mu}_c\|.

Here arg⁡min⁡c\arg\min_c means "the class cc that makes the expression smallest". For the new point x=(2,2)\mathbf{x} = (2, 2), ∥x−μA∥=02+12=1\|\mathbf{x} - \boldsymbol{\mu}_A\| = \sqrt{0^2 + 1^2} = 1 and ∥x−μB∥=02+32=3\|\mathbf{x} - \boldsymbol{\mu}_B\| = \sqrt{0^2 + 3^2} = 3. The smaller is 1, so y^=A\hat{y} = A.

Adding examples without storing them

The average can be updated one example at a time. If class cc already has nn examples with mean μ\boldsymbol{\mu}, and a new example x\mathbf{x} arrives, the new mean is

μnew=μ+1n+1(x−μ).\boldsymbol{\mu}^{\text{new}} = \boldsymbol{\mu} + \frac{1}{n+1}\left(\mathbf{x} - \boldsymbol{\mu}\right).

Check with class A: start from one example (1, 1), so μ=(1,1)\boldsymbol{\mu} = (1, 1) and n=1n = 1. Add (3, 1). Then μnew=(1,1)+12((3,1)−(1,1))=(1,1)+(1,0)=(2,1)\boldsymbol{\mu}^{\text{new}} = (1, 1) + \tfrac12 \big((3, 1) - (1, 1)\big) = (1, 1) + (1, 0) = (2, 1), which matches Step 2.

Where the boundary lies

Two prototypes give a boundary where the two distances are equal. Setting ∥x−μA∥2=∥x−μB∥2\|\mathbf{x} - \boldsymbol{\mu}_A\|^2 = \|\mathbf{x} - \boldsymbol{\mu}_B\|^2 and expanding gives

2 x⊤(μB−μA)=∥μB∥2−∥μA∥2.2\,\mathbf{x}^{\top}(\boldsymbol{\mu}_B - \boldsymbol{\mu}_A) = \|\boldsymbol{\mu}_B\|^2 - \|\boldsymbol{\mu}_A\|^2.

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: μB−μA=(0,4)\boldsymbol{\mu}_B - \boldsymbol{\mu}_A = (0, 4) and the right side is (4+25)−(4+1)=24(4 + 25) - (4 + 1) = 24. So 8x2=248 x_2 = 24 and x2=3x_2 = 3, which is the midpoint between the prototypes at heights 1 and 5.

Worked examples

Example
1. Tiny case: one number per input

Class A has examples 2 and 4. Class B has examples 10 and 12.

  1. The prototype of A is (2+4)/2=3(2 + 4)/2 = 3. The prototype of B is (10+12)/2=11(10 + 12)/2 = 11.
  2. New input x=6x = 6. Its distance to 3 is ∣6−3∣=3|6 - 3| = 3. Its distance to 11 is ∣6−11∣=5|6 - 11| = 5.
  3. The smaller distance is 3, so label x=6x = 6 as A.
  4. New input x=8x = 8. Its distance to 3 is 5. Its distance to 11 is 3.
  5. Now the smaller distance is 3 to B, so label x=8x = 8 as B.

Interpretation: the boundary is the midpoint, (3+11)/2=7(3 + 11)/2 = 7. Inputs below 7 go to A and inputs above 7 go to B.

Example
2. Standard case: two coordinates, a boundary check

Use the classes from the walkthrough, with prototypes μA=(2,1)\boldsymbol{\mu}_A = (2, 1) and μB=(2,5)\boldsymbol{\mu}_B = (2, 5).

  1. New input x=(2,4)\mathbf{x} = (2, 4). Its squared distance to A is 02+32=90^2 + 3^2 = 9.
  2. Its squared distance to B is 02+12=10^2 + 1^2 = 1.
  3. Since 1<91 \lt 9, the nearest prototype is B, so label x\mathbf{x} as B.
  4. Check the boundary from the math section: the line is x2=3x_2 = 3. The point has x2=4>3x_2 = 4 \gt 3, so it lies on the B side. The two results agree.
  5. Now take x=(1,2)\mathbf{x} = (1, 2). Its squared distance to A is 12+12=21^2 + 1^2 = 2, and to B it is 12+32=101^2 + 3^2 = 10. Since 2<102 \lt 10, label it A. Here x2=2<3x_2 = 2 \lt 3, 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.

Example
3. Realistic: a new class arrives

An image system knows two classes, A (prototype (2,1)(2, 1)) and B (prototype (2,5)(2, 5)). Three labelled examples of a new class, bird, arrive: (0,6)(0, 6), (2,6)(2, 6) and (4,6)(4, 6). The model is not retrained. The new prototype is their average, and the old prototypes are left as they are.

  1. Bird prototype: first coordinates (0+2+4)/3=2(0 + 2 + 4)/3 = 2; second coordinates (6+6+6)/3=6(6 + 6 + 6)/3 = 6. So the prototype is (2,6)(2, 6).
  2. A new input x=(2,6.5)\mathbf{x} = (2, 6.5) arrives. Its squared distance to A is 02+5.52=30.250^2 + 5.5^2 = 30.25.
  3. Its squared distance to B is 02+1.52=2.250^2 + 1.5^2 = 2.25.
  4. Its squared distance to bird is 02+0.52=0.250^2 + 0.5^2 = 0.25.
  5. The smallest is 0.25, so label x\mathbf{x} as bird.

Try it: the same three prototypes, A at (2,1)(2, 1), B at (2,5)(2, 5) and bird at (2,6)(2, 6). Classify x=(3,4.5)\mathbf{x} = (3, 4.5) using squared distances. Give the label.

Answer: Squared distance to A: 12+3.52=1+12.25=13.251^2 + 3.5^2 = 1 + 12.25 = 13.25. Squared distance to B: 12+0.52=1+0.25=1.251^2 + 0.5^2 = 1 + 0.25 = 1.25. Squared distance to bird: 12+1.52=1+2.25=3.251^2 + 1.5^2 = 1 + 2.25 = 3.25. 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
One prototype vector: the average of the feature vectors of that class's examples. Its coordinates are the per-coordinate averages over the class.
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
Distance to A is 02+32=3\sqrt{0^2 + 3^2} = 3. Distance to B is 02+12=1\sqrt{0^2 + 1^2} = 1. The smaller is 1, so the label is B.
QuestionWhy can a nearest-class-mean classifier add a new class without retraining on the old data?Show answer
Each prototype depends only on the examples of its own class. A new class needs only its own average, and the existing prototypes are unchanged, so no past training examples are needed.
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
The single average can sit in the empty space between the clusters, far from both. Inputs near a cluster may then be closer to another class's prototype. A remedy is to use several prototypes per class, or a method that keeps all examples, such as the nearest-neighbor rule.

Sources