How does k-means clustering differ from k-nearest neighbours?

Last updated 11 November 2026

Despite the shared "k", they solve different problems. k-means is an unsupervised clustering algorithm where k is the number of clusters. k-NN is a supervised (or lazy) predictor where k is the number of neighbors consulted to label a new point.

  • k-means:

    • Iteratively assigns points to the nearest centroid, then recomputes the centroids. This minimizes within-cluster sum of squares.

    • Has a real training phase and outputs k centroids plus a cluster per point. No labels are needed.

  • k-NN:

    • No training: it stores the labeled data. At query time it finds the k closest points and votes (classification) or averages (regression).

    • Cost sits at prediction time. KD-trees, ball trees, or approximate NN indexes speed it up.

  • What they share: Both depend on a distance metric, so both need feature scaling and both suffer in high dimensions.

  • Choosing k:

    • k-means: elbow method or silhouette score.

    • k-NN: cross-validated predictive performance. Small k means high variance; large k means high bias.

  • https://medium.com/@xiaxiami/from-scratch-to-insight-implementing-and-comparing-k-means-and-k-nearest-neighbors-in-python-e19139a2a3ba
  • https://datarekha.com/interview/machine-learning/kmeans-vs-knn-difference/
  • https://www.rohan-paul.com/p/ml-interview-q-series-how-do-k-means
All Clustering interview questions