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 points and want to split them into groups. Describe a split by giving every point a cluster indicator : means point goes in box 3. Any choice of is a partition.
There are a great many partitions: each point has choices, so up to 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 is
the average of the points assigned to it (the indicator is 1 when point is in cluster and 0 otherwise). The score of the partition is the total squared distance from every point to its own cluster's mean:
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, , and .
- Partition A: and . The means are 2 and 10.5. The score is .
- Partition B: and . The means are 1.5 and 8. The score is .
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 . With only finitely many partitions, we could in principle score each one. But there are about of them: with and points, around , 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 and . 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).
- Initialise: put every point in some cluster, giving . (How to do this well is the subject of a later chapter.)
- Repeat until nothing changes:
- Update the means: for each cluster , set to the mean of the points currently assigned to it.
- Reassign: move every point to the cluster whose mean is nearest, 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.
Four questions
Stated this simply, the algorithm raises four questions, which the next two chapters answer.
- Does it always stop? Could points keep jumping between clusters forever?
- What shape are the clusters it finds?
- How should it be started? Different starting partitions can lead to different answers.
- How do we choose K? Sometimes it is given (a teacher sorting students into five grades knows K = 5); usually it is not.