No labels this time, only dots. Can the groups be found? Below, three centres go looking. Before it plays: can the number under the picture ever go up?
Finding groups in data
90 unlabelled dots; three centres, each dot joining the nearest and each centre moving to the mean of its dots.
90 dots with no labels at all. Three centres start bunched together below them.
Describe this picture
One plane, feature 1 across from −4 to 4 and feature 2 up from −3.5 to 4.5, with 90 grey dots in three clouds, lower left, lower right and top, and no labels at all. Three centres, marked with crosses, start bunched together below the dots. In the clip two steps take turns. Assign: every dot takes the colour of its nearest centre; the first time, the middle centre is nearest to only a few. Move: every centre glides to the mean of its dots. After each step the total squared distance from the dots to their centres is listed beside the plane: 1216.7 after the first colouring, then 211.9, 115.1, 63.8, 59.3 and 59.1, never rising. By then each centre sits in one cloud with its 30 dots. A last assignment changes no dot’s colour, and the run stops. Readouts give the total squared distance, the dots that changed colour in the last assignment, 90, then 22, 1 and finally 0, and the steps taken. There is a play button and no other control.
The two steps
There are points and no labels: nobody has said which point belongs where. Choose a number of groups, or clusters, , and a starting place for each centre, , numbered by . Then repeat two steps.
Assign. Give each point to its nearest centre:
so is the cluster of point . If two centres are equally near, the point goes to the lower . This is Nearest neighbours with , asked of the centres instead of the training examples.
Update. Move each centre to the mean of its points: the average of their first coordinates and the average of their second. A centre with no points keeps its place.
Stop when an assignment changes nothing. That is k-means.
Why it settles
Measure how well the centres fit by the total squared distance from each point to its own centre,
Now the answer to the question. The cost can never go up, and each step is the reason.
- The assign step gives each point the nearest centre. Its squared distance can only shrink or stay.
- The update step moves each centre to the mean of its points. The mean is the one point with the smallest total squared distance to a set of points, the best constant of Models and loss worked in each coordinate. So no centre position does better for those points.
A cost that never rises, with only finitely many ways to split points into groups, has to stop changing. In the clip it falls from 1216.7 to 59.1 in six steps.
In probability and statistics · the mean as the best centre
Describing data in the statistics course compares the mean with the median. Under squared distance the mean is the best single summary, so it is the centre k-means uses.
Starts and the choice of k
The cost settles, but not always at the lowest cost there is. It stops at the first split that no single step can improve, and where that is depends on the start.
the start below the dots
total 59.1 · groups of 30, 30, 30
another start
total 231.6 · groups of 15, 15, 60
Describe this picture
Two plots of the same 90 dots, coloured by group, with a cross at each centre. On the left, “the start below the dots”, the clip’s run: one group for each cloud, total 59.1. On the right, “another start”, from centres at , and : two groups of 15 share one cloud and one group of 60 covers the other two clouds, total 231.6. Both runs have stopped: each centre is the mean of its dots, and no dot is nearer another centre. There is no control.
So different starts can end on different groups: here at nearly four times the cost. Practical programs run k-means from several starts and keep the run with the lowest cost. A good way to start, called k-means++, picks the first centre at a random data point and each next one at a data point chosen at random, with points far from every centre so far more likely to be picked.
The number is a choice too, and the cost cannot make it:
Describe this picture
A curve of the total squared distance of the clip’s 90 dots against the number of groups , from 1 to 90 on a logarithmic axis, each point the best of ten runs. It starts at 487.6 for one group, falls to 241.4 at and steeply to 59.1 at , where the bend is marked, then slowly, to 49.3 at and all the way down to 0 at , one group per dot. The caption says that more groups always lower the cost, that the lowest cost cannot choose , and that real data rarely bend as clearly as these three clouds. There is no control.
More centres never cost more: give one more centre a place on any point and the cost can only fall. With one centre per point, every point is its own centre and the cost is 0. So the lowest cost always picks the largest , and it means nothing. Here the bend at stands out, because the data were made from three clouds. Real data rarely bend so clearly, and choosing often needs knowledge of what the groups are for.
Watch out
k-means does not find the number of groups. Its cost falls whenever rises, so a lower cost is no reason to choose a larger .
What it cannot find
Every point goes to its nearest centre, so the boundary between two clusters is the line of points equally far from both centres: a straight line at right angles to the segment between them, through its middle. k-means can only cut the plane with such straight lines, and it favours round groups of similar size.
two crescents
two long bands
a big cloud and a small one
Describe this picture
Three plots, each run with two groups and split by a dashed straight line halfway between the two centres into two shaded halves, with the dots coloured by the group k-means gives them and a cross at each centre. On the left, “two crescents”, the 100 dots of Nearest neighbours with their labels hidden: the line cuts across both crescents, so each group holds dots of both: 16 of class A and 41 of class B in one group, 34 and 9 in the other. In the middle, “two long bands”, one above the other: an upright line cuts them into a left half and a right half. On the right, “a big cloud and a small one”: the small cloud’s group also takes the near edge of the big cloud. The caption begins “k-means finds round groups, not curved ones”. There is no control.
The same goes for long groups, which k-means cuts across rather than along, and for a small, tight group next to a large, spread-out one: the boundary runs midway between the two centres, so the small group’s centre takes the large group’s edge points. Gaussian mixtures allow stretched groups and groups of different sizes.
Worked example
Six points, , , , , and , and , starting from a poor place: and , both in the lower group.
Assign. and are nearest ; sits on , and the three upper points are nearer too. The squared distances to the chosen centres are , , , , and : cost 115.
Update. moves to the mean of and , which is . moves to the mean of the other four, . Cost 30.
Assign. is now nearer : squared distance , against to . It changes group. Cost 10.125.
Update. , the mean of the lower three, and , the mean of the upper three. Cost .
Assign. No point changes group, so the run stops. The costs were .
Both centres in one place. Start both centres at the same point. Every point is then equally near both, so the tie rule gives all of them to , and gets none. The update moves to the mean of all the data while keeps its place. Now the two differ, and the next assignment splits the data. On the clip’s 90 dots, with both centres starting at , that second split is 59 and 31 dots.
Where you’ll meet this
Customers. Shops group customers by what they buy and how often, to see which kinds of customer they have.
Colour palettes. A photo’s pixels are points in a space of red, green and blue; k-means with finds 16 colours, and each pixel is drawn in its centre’s colour.
Codebooks. Speech and image coders send the number of the nearest centre instead of the whole vector; the centres, the codebook, are often found by k-means.
Operating modes. A machine’s sensor logs fall into groups such as idle, running and starting up, found without anyone labelling the readings.
In signal processing · rounding a vector
Quantization in the signal processing course rounds a vector to the nearest point of a regular grid. A codebook found by k-means is a grid fitted to the data: its points sit where the vectors are, and each vector is rounded to its nearest centre.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Assign | : the nearest centre | ties to the lower |
| Update | = mean of its points | an empty cluster keeps its centre |
| Cost | never rises | |
| Stop | an assignment changes nothing | always reached |
| Start | the answer depends on it | several starts, keep the lowest cost |
| Choice of | the cost falls as rises | 0 at one centre per point |
| Boundaries | straight lines between centres | round groups of similar size |