The Nearest Centroid Classifier is a simple alternative to k-Nearest Neighbors (k-NN) for similarity-based classification. It is also known as the Mean Difference Classifier.
The key idea of this classifier is to assign a test instance to the class whose centroid (mean point) is closest to the test instance using a distance metric (usually Euclidean distance).
Algorithm : Nearest Centroid Classifier
Inputs: Training dataset T, Distance metric d, Test instance t
Output: Predicted class or category
Steps:
- Compute the mean (centroid) of each class.
- Compute the Euclidean distance between the test instance and the centroid of each class.
- Predict the class with the smallest distance to the test instance.
Given Data :
x | y | Class |
---|---|---|
3 | 1 | A |
5 | 2 | A |
4 | 3 | A |
7 | 6 | B |
6 | 7 | B |
8 | 5 | B |
We are given a test instance (6, 5) and asked to classify it using Nearest Centroid Classifier.

Step 3: Prediction
- Since the distance to class B is smaller, the test instance (6, 5) is closer to class B.
→ Predicted class: B