ML0061 KNN and K-means

What are the key differences between KNN and K-means?

Answer

KNN (K-Nearest Neighbors) is a supervised algorithm that classifies data using the labels of its nearest neighbors: prediction from historical labeled data. K-means is an unsupervised clustering technique that groups data purely by similarity, using no labels at all. Despite the shared “K”, they solve different problems.

(1) Learning Type: KNN is supervised (classification/regression); K-means is unsupervised (clustering).
(2) Objective: KNN predicts a new sample’s label from the majority vote (or average) of its K nearest neighbors; K-means partitions the dataset into K clusters by minimizing intra-cluster distance.
(3) Training: KNN has no explicit training: it stores the entire dataset; K-means iteratively learns cluster centroids.
(4) Prediction Cost: KNN is expensive at prediction (distance to every training point, sort, take the top K, then vote/average); K-means is cheap: distances to K centroids, assign the nearest.
(5) Distance Use And Output: KNN uses distance to find neighbors and outputs a label or value; K-means uses distance to assign points to centroids and outputs cluster assignments plus centroids.

FeatureK-Nearest Neighbors (KNN)K-Means
TypeSupervised LearningUnsupervised Learning
TaskClassification, RegressionClustering
Data RequiredLabeled dataUnlabeled data
Training PhaseStores all training data (lazy learner)Iterative centroid calculation
Prediction PhaseFinds K nearest neighbors and assigns label/valueAssigns new points to closest cluster centroid
Compute CostHigh at prediction (distance calculations for each new point)High at training (iterative updates); low at prediction (centroid assignment)
GoalPredict label/value for new dataGroup data into K clusters
OutputClass label or predicted valueData points assigned to clusters

Mathematical Formulation:
\hat{y} = \arg\max_{c \in \mathcal{C}} \; \sum_{i=1}^{K} \mathbb{1}(y_i = c)
\min_{C_1, \ldots, C_K} \; \sum_{k=1}^{K} \sum_{x \in C_k} \|x - \mu_k\|^2

Where:

  • KNN (first line): \hat{y} is the predicted class, \mathcal{C} the class set, y_i the label of the i-th nearest neighbor, and \mathbb{1}(\cdot) the vote-counting indicator.
  • K-means (second line): C_k is cluster k and \mu_k its centroid; the algorithm minimizes total within-cluster squared distance: no labels appear anywhere.
Two panels on crescent moon data showing KNN separating the moons correctly while K-means cuts through them

Figure 1: Same data, different goals: with labels, KNN’s local votes trace the two crescents perfectly (left); without labels, K-means can only split by nearest-centroid geometry and slices through both moons (right). KNN performs well here thanks to local decision-making; K-means fails because it assumes spherical clusters with linear boundaries.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *