ElemedexA pocket encyclopedia of AI architectures
No. 008

k-means

Lloyd's algorithm

Centroid

It walks into a party of strangers and, with nobody telling it who is who, splits them into k circles and stands in the middle of each. Then it moves to the center, a few guests switch circles, and it repeats until nobody moves. The one thing it cannot decide is how many circles.

Height
0 (no layers)
Weight
k vectors
Parameters
k × d numbers: the centroids
Context
1 vector per point
Training data
unlabeled points, any number
Year
1967
Generation
1 · Cybernetics and symbols
Creators
James MacQueen (1967), after Stuart Lloyd (Bell Labs, 1957; published 1982)
Kind
Algorithm

Base stats

Total
142

Approximate, ordinal scales meant for comparing species with each other; the hard numbers are in the data sheet.

Weaknesses

  • k must be fixed in advance, and on the training data more clusters always means less error.
  • It only guarantees a local optimum: the result depends on the initial seeds.
  • It presupposes compact, roughly spherical groups separable by Euclidean distance; outliers drag the centers around.
  • It forces every point into some cluster: it has no “noise” category.

Habitat

It was born at Bell Labs as a technique for quantizing signals and got its name at a statistics symposium in Berkeley. Today it clusters data in nearly every discipline and, in language processing, document embeddings, usually as the first thing to try before something subtler.

Moves

Abilities

Assign and average
It alternates two steps (assign each point to the nearest center, move each center to the mean of its group), and neither can increase the total squared distance.
Prototypes
Each group is represented by a single vector, its centroid: a summary that may coincide with no actual member.
Convex partition
Each center owns the region of space closer to it than to any other; these cells are always convex.

Anatomy

Lloyd's algorithm: the assignment step splits space into convex cells around the centers (a Voronoi tessellation); the update step moves each center to the average of its cell. It always converges, but to a local optimum that depends on the seeds.

Evolution chain

No known evolutions.

008 k-means

k-means is the classic clustering algorithm: given a set of unlabeled points and a number k, it splits them into k groups, each represented by its center. It is the first species in this Pokédex that learns without a teacher (unsupervised learning): there is no class to predict, only "natural" groups that presumably reflect some mechanism in the domain (Witten et al., 2017, p. 141).

How it works

Each point is a vector. The procedure described by Witten et al. (2017) has five steps (2017, pp. 142–143):

  1. Fix k in advance.
  2. Pick k points at random as initial centers.
  3. Assign each point to the nearest center (Euclidean distance).
  4. Replace each center with the mean, or centroid, of its points: hence "means."
  5. Repeat steps 3 and 4 until no assignment changes.

In the book's example, fifteen points with k = 3 settle down in a few rounds (Witten et al., 2017, p. 143). Convergence is guaranteed because no step makes things worse: the centroid is the point that minimizes the sum of squared distances within a group, and assigning each point to the nearest center reduces it too; the overall effect is to minimize the total squared distance (Witten et al., 2017, p. 144). In today's terms, it is a loss function optimized in turns.

The name comes from MacQueen (1967). The alternating procedure had already been formulated by Stuart Lloyd at Bell Labs in 1957, for quantizing signals in pulse-code modulation (PCM), although it was published only in 1982 (Lloyd, 1982); hence the name "Lloyd's algorithm."

What changed

Compared with k-nearest neighbors, which keeps every example, k-means summarizes: each group is condensed into a single vector, and its parameters are just k × d numbers (k centers in d dimensions). The assignment step carves space into convex cells, one per center: the same geometry kNN uses with all its examples, but with only k representatives.

Limits

The minimum it finds is local: the result depends on the initial seeds, and small changes can produce very different clusterings. Witten et al. (2017) give a minimal example: four points at the corners of a rectangle; if the seeds fall at the midpoints of the long sides, the solution is stable and wrong (2017, p. 144). The usual remedy is to run it several times and keep the best result, or to seed with k-means++, which picks each seed with probability proportional to its squared distance from those already chosen (Witten et al., 2017, p. 144).

The deeper problem is k, a hyperparameter. On the training data, the "best" clustering by total distance is always one cluster per point; one must penalize extra clusters (for instance with minimum description length) or test successive splits (Witten et al., 2017, p. 146). Add to this its inductive bias: it assumes compact, convex groups.

With text, the typical pipeline is to turn documents into embeddings, reduce their dimension, and only then cluster (Alammar & Grootendorst, 2024, p. 139). k-means is a common choice, but it requires knowing k; density-based methods such as HDBSCAN decide the number of clusters themselves and leave outliers unassigned (Alammar & Grootendorst, 2024, p. 143).

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Buckner, C. J. (2024). From deep learning to rational machines: What the history of philosophy can teach us about the future of artificial intelligence. Oxford University Press. https://doi.org/10.1093/oso/9780197653302.001.0001 §
  • Gärdenfors, P. (2000). Conceptual spaces: The geometry of thought. MIT Press. https://doi.org/10.7551/mitpress/2076.001.0001 §
  • Lloyd, S. P. (1982). Least squares quantization in PCM. IEEE Transactions on Information Theory, 28(2), 129–137. https://doi.org/10.1109/TIT.1982.1056489 §
  • MacQueen, J. (1967). Some methods for classification and analysis of multivariate observations. In L. M. Le Cam & J. Neyman (Eds.), Proceedings of the Fifth Berkeley Symposium on Mathematical Statistics and Probability (Vol. 1, pp. 281–297). University of California Press. §
  • Posner, M. I., & Keele, S. W. (1968). On the genesis of abstract ideas. Journal of Experimental Psychology, 77(3, Pt. 1), 353–363. https://doi.org/10.1037/h0025953 §
  • Rosch, E. (1975). Cognitive representations of semantic categories. Journal of Experimental Psychology: General, 104(3), 192–233. https://doi.org/10.1037/0096-3445.104.3.192 §
  • Witten, I. H., Frank, E., Hall, M. A., & Pal, C. J. (2017). Data mining: Practical machine learning tools and techniques (4th ed.). Morgan Kaufmann. https://doi.org/10.1016/C2015-0-02071-8 §