K-means is an unsupervised algorithm that partitions points into clusters by minimizing within-cluster squared distance to cluster centroids. It is a hard-assignment special case of the soft Gaussian Mixture Models.

Objective

Minimize the total within-cluster sum of squares over assignments and centroids :

This is NP-hard in general, so we use iterative local optimization.

Lloyd’s Algorithm

  1. Initialize centroids.
  2. Assignment step: assign each point to its nearest centroid.
  3. Update step: set each centroid to the mean of its assigned points.
  4. Repeat until assignments stop changing.
  • Each step never increases , so it converges to a local minimum.
  • Cost per iteration is .

Initialization (k-means++)

  • Random init can yield poor local minima; run multiple restarts and keep the lowest .
  • k-means++ spreads initial centers: pick the first uniformly, then each next center with probability proportional to squared distance from the nearest chosen center.
  • Gives an expected approximation guarantee and faster convergence.

Choosing k

  • Elbow method: plot vs and pick the bend.
  • Silhouette score: measures cohesion vs separation.
  • Domain knowledge or downstream task performance.

Limitations

  • Assumes spherical, equally sized clusters; struggles with elongated or varying-density shapes.
  • Sensitive to scaling and outliers (means are not robust).
  • Must fix in advance; only finds local optima.

Relation to GMMs

K-means is the limiting case of a GMM with isotropic covariances as , replacing soft responsibilities with hard nearest-centroid assignments. Both are optimized by an EM-style alternation.