Skip to content
mlmentorship

k-means clustering

Partition n points into k clusters by minimizing within-cluster variance. Lloyd's algorithm: alternate assigning points to nearest center and recomputing centers.

Published · 6 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

Trace one Lloyd iteration by assigning each point to its nearest fixed centroid, then replacing each centroid with the arithmetic mean of its fixed assigned points.

Preparing the visual…

Summary

k-means partitions data points into clusters by minimizing the sum of squared distances from each point to its cluster’s centroid:

Lloyd’s algorithm (1957) alternates: (a) assign each point to its nearest centroid; (b) recompute each centroid as the mean of its assigned points. Iterate to convergence.

k-means is the canonical clustering algorithm: per iteration, easy to implement, and a good first attempt at any unsupervised structure problem. It is used directly (customer segmentation, vector-quantization codebooks) and as a building block (initialization for GMMs, anchor selection for object detection, sub-sampling for retrieval index training).

The algorithm

Initialize centroids (random points or k-means++ for stability). Then repeat:

  1. Assignment: for each .
  2. Update: for each .

Learning objective

What stays fixed in each half of a Lloyd iteration?

Assignment step with two fixed initial centroids Three circular observations at data coordinates 50 comma 80, 70 comma 110, and 90 comma 80 are assigned to initial centroid A at 110 comma 130. Three square observations at 230 comma 70, 250 comma 100, and 270 comma 70 are assigned to initial centroid B at 210 comma 120. For the circular observation at 90 comma 80, squared distance to A is 2900 and squared distance to B is 16000, so it joins A. Centroids do not move during this step. 1 · ASSIGN · HOLD CENTROIDS FIXED d² to A = 2,900 d² to B = 16,000 A₀ (110, 130) B₀ (210, 120) ○ joins nearest A₀ · □ joins nearest B₀ Only membership changes.
Update step moves each centroid to its assigned points' mean The same three circles and three squares retain their assignments. Initial centroid A moves from 110 comma 130 to the circular points' arithmetic mean at 70 comma 90. Initial centroid B moves from 210 comma 120 to the square points' arithmetic mean at 250 comma 80. Labelled arrows connect each old center to its new mean. Assignments do not change during this step. 2 · UPDATE · HOLD ASSIGNMENTS FIXED A₁ = (70, 90) B₁ = (250, 80) A₁ = ((50+70+90)/3, (80+110+80)/3) B₁ = ((230+250+270)/3, (70+100+70)/3)
Read it this way: in the assignment panel, compare distances while A₀ and B₀ stay fixed; circles and squares encode the resulting memberships without relying on color. In the update panel, freeze those memberships and move each × to the arithmetic mean of its three points. Alternating these two objective-nonincreasing steps reaches a fixed point, but the result can still be only a local minimum. Original construction checked against Lloyd's least-squares quantization paper and the scikit-learn k-means guide.

Stop when assignments don’t change (or change below a threshold). Convergence is guaranteed in finite steps but only to a local minimum. K-means is not convex.

k-means++ initialization

Random initialization sometimes converges to bad local minima. k-means++ (Arthur & Vassilvitskii, 2007):

  1. Pick uniformly at random.
  2. For : pick from data with probability proportional to . Points far from existing centers are more likely to be picked.

Gives an -approximation to the optimum in expectation. Used as default initialization in scikit-learn and most modern implementations.

Choosing

There is no universally right answer. Heuristics:

  • Elbow method: plot total within-cluster sum of squares vs ; look for the “elbow” where adding more clusters stops paying off.
  • Silhouette score: average similarity of each point to its own cluster vs. nearest other cluster; pick maximizing it.
  • Gap statistic: compare to clustering on uniform random data of the same shape.
  • Domain knowledge: often the right answer.

For very large data, run k-means with a few values of in parallel and pick the one with the best silhouette on a subsample.

Assumptions

k-means implicitly assumes:

  • Spherical, equal-variance clusters (penalizes non-spherical clusters by splitting them).
  • Equal cluster sizes (large clusters are sometimes split, small ones merged).
  • Euclidean distance is meaningful in the input space.

When clusters are elongated, of unequal density, or non-convex, k-means fails. Try GMMs (allows ellipsoidal clusters), DBSCAN (density-based, arbitrary shapes), or spectral clustering.

Mini-batch k-means

For huge , full assignment is expensive. Mini-batch k-means (Sculley, 2010): each iteration uses a small random subset to update centroids. Trades accuracy for huge speedup.

When k-means is and isn’t appropriate

SettingVerdict
Roughly spherical clusters in low-dExcellent
Vector quantization codebookExcellent (k = codebook size)
Image segmentation by colorGood (k = number of colors)
Customer segmentation on raw featuresTry, but standardize first
Text or sparse dataUse spherical k-means (cosine distance)
Non-convex shapes (moons, rings)Fails; use DBSCAN or spectral
Large (thousands)Use HNSW-accelerated variants

Common pitfalls

  • Forgetting to standardize features. k-means uses Euclidean distance; features with larger scale dominate.
  • Random initialization without k-means++. Different runs give different clusterings; report consensus or use k-means++.
  • Treating cluster IDs as meaningful. Cluster 0 in one run may be cluster 7 in the next; permutation invariance.
  • Using k-means on categorical data without thought. Use k-modes, k-prototypes, or one-hot + k-means with care.
  • Reporting one clustering as “the answer.” Run multiple times, take the best by within-cluster SS.