Clustering and the K-Means Objective

Clustering partitions n points into K groups. Scoring a partition by the total squared distance of points to their cluster means gives the k-means objective, which is NP-hard to minimise exactly. Lloyd's algorithm (assign each point to its nearest mean, recompute the means, repeat) is the standard heuristic.

Machine Learning Techniques

PCA describes a data set by the directions along which it spreads. Sometimes the most important structure is not a direction but a set of groups. Picture three tight clumps of points lying roughly along a line. PCA finds the line and projects every point onto it, but the projections still sit in three clumps, and PCA has nothing to say about them. Finding those clumps is clustering.

A partition and its score

We have nn points x1,…,xn∈Rdx_1, \dots, x_n \in \mathbb{R}^d and want to split them into KK groups. Describe a split by giving every point a cluster indicator zi∈{1,…,K}z_i \in \{1, \dots, K\}: zi=3z_i = 3 means point ii goes in box 3. Any choice of z1,…,znz_1, \dots, z_n is a partition.

There are a great many partitions: each point has KK choices, so up to KnK^n of them. To pick one we need to score them. A natural requirement is that each cluster should be tight: its points should sit close to one another. Measure that by each point's squared distance from the mean of its own cluster. The mean of cluster kk is

μk=∑i=1n1[zi=k] xi∑i=1n1[zi=k],\mu_k = \frac{\sum_{i=1}^{n} \mathbf{1}[z_i = k]\, x_i}{\sum_{i=1}^{n} \mathbf{1}[z_i = k]},

the average of the points assigned to it (the indicator 1[zi=k]\mathbf{1}[z_i = k] is 1 when point ii is in cluster kk and 0 otherwise). The score of the partition is the total squared distance from every point to its own cluster's mean:

F(z1,…,zn)=∑i=1n∥xi−μzi∥2.F(z_1, \dots, z_n) = \sum_{i=1}^{n} \big\lVert x_i - \mu_{z_i} \big\rVert^2 .

Smaller is better. This is the k-means objective (also called the within-cluster sum of squares, or SSE).

A small example

Take five points on a number line, x=1,2,3,10,11x = 1, 2, 3, 10, 11, and K=2K = 2.

  • Partition A: {1,2,3}\{1, 2, 3\} and {10,11}\{10, 11\}. The means are 2 and 10.5. The score is (1+0+1)+(0.25+0.25)=2.5(1 + 0 + 1) + (0.25 + 0.25) = 2.5.
  • Partition B: {1,2}\{1, 2\} and {3,10,11}\{3, 10, 11\}. The means are 1.5 and 8. The score is (0.25+0.25)+(25+4+9)=38.5(0.25 + 0.25) + (25 + 4 + 9) = 38.5.

Partition A is far better, matching what the eye sees.

Why not just try every partition?

The goal is to find the partition with the smallest FF. With only finitely many partitions, we could in principle score each one. But there are about KnK^n of them: with K=2K = 2 and n=1,000n = 1{,}000 points, around 210002^{1000}, a number with more than 300 digits. Exact k-means is NP-hard, so no algorithm is expected to solve every instance in time polynomial in nn and KK. We settle for a fast heuristic that finds a good partition, usually not provably the best.

Lloyd's algorithm

The standard heuristic is Lloyd's algorithm, usually just called k-means (strictly, k-means is the problem and Lloyd's algorithm is one way of attacking it).

  1. Initialise: put every point in some cluster, giving z1(0),…,zn(0)z^{(0)}_1, \dots, z^{(0)}_n. (How to do this well is the subject of a later chapter.)
  2. Repeat until nothing changes:
    • Update the means: for each cluster kk, set μk(t)\mu^{(t)}_k to the mean of the points currently assigned to it.
    • Reassign: move every point to the cluster whose mean is nearest, zi(t+1)=arg⁡min⁡k∥xi−μk(t)∥2,z^{(t+1)}_i = \arg\min_{k} \big\lVert x_i - \mu^{(t)}_k \big\rVert^2 , keeping it where it is if its current mean is already among the nearest (so that ties never cause pointless moves).

When a full pass moves no point, every point is already closest to its own cluster's mean, and the algorithm stops: it has converged.

Three panels of the same scatter of points: first, three starting centres placed among the points; second, every point coloured by its nearest centre with the boundaries between regions drawn; third, each centre moved to the mean of its coloured points
One round of Lloyd's algorithm: assign every point to its nearest centre, then move every centre to the mean of its points. Repeat until no point changes cluster.

Four questions

Stated this simply, the algorithm raises four questions, which the next two chapters answer.

  1. Does it always stop? Could points keep jumping between clusters forever?
  2. What shape are the clusters it finds?
  3. How should it be started? Different starting partitions can lead to different answers.
  4. How do we choose K? Sometimes it is given (a teacher sorting students into five grades knows K = 5); usually it is not.
Try it yourself
Machine Learning Lab: k-means →
Step through Lloyd's algorithm one assign-and-average round at a time and watch the centres travel and the SSE fall.
EasyK-means

Compute the k-means objective for the points 0, 4, 6, 9 on a line, split as {0, 4} and {6, 9}, then improve it.

HardK-meansObjective

Why does the k-means objective use squared Euclidean distance to the mean, and what changes if you use plain (unsquared) distance?

MediumCombinatoricsClustering

How many ways are there to put 5 labelled points into 3 boxes if empty boxes are allowed? Why is the exact count of clusterings smaller?