Back to the 2023 paper

Module 4: Clustering, K-means, K-medoids, Density-based Hierarchical, Spectral .

20237m

What is the difference between K-NN and K-means clustering algorithms?

Worked SolutionAI Assisted

K-NN vs K-means:

Aspect K-NN (K-Nearest Neighbors) K-means (Clustering)
Type Supervised, instance-based, lazy classifier/regressor Unsupervised clustering
Purpose Predict label yy for query using labeled neighbors Group unlabeled data into kk clusters minimizing SSE
Input Labeled training (xi,yi)(x_i,y_i) Unlabeled xix_i only, need kk preset
Working For query xqx_q, find kk nearest labeled points by distance, majority vote / average: y^=arg⁡max⁡c∑I(yi=c)\hat y = \arg\max_c \sum \mathbb{I}(y_i=c) Iteratively assign points to nearest centroid μj\mu_j, recompute μj=\mu_j= mean, repeat until convergence
Training No model building, stores data Builds model (centroids)
Prediction cost O(nd)O(n d) per query (slow) O(kd)O(k d) after training (fast assign)
Output Single label per query Cluster id for all points
Choosing kk kk = neighborhood size via CV (bias-variance) kk = number clusters via elbow/silhouette
Example Classify email as spam using 5 nearest labeled emails Segment customers into 3 groups without labels

Common confusion: Both use distance and kk, but meanings completely different: kk in K-NN = neighbors to vote, kk in K-means = clusters. K-NN needs labels, K-means does not. K-NN predicts new point, K-means discovers structure.

When to use: Labels available + need classification → K-NN. No labels + need grouping → K-means.

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.