k-Means
Find groups on your own, by moving centres until they settle.
Drag the stands, or press Step and Run. Everyone walks to their nearest stand.
- a stand: drag it
- a person, coloured by the stand they walk to
- khow many stands
Person 1: A 25.4 m · B 36.7 m · C 39.4 m → walks to A
J = 6,971 m² → typical walk √(J ÷ 24) = 17.0 m
| 1 | 21.4 | 58.8 | A | 25.4 |
| 3 | 27.1 | 54.2 | A | 18.1 |
| 4 | 26.8 | 58.2 | A | 20.8 |
| 6 | 31.3 | 14.9 | A | 31.0 |
| 8 | 38.7 | 28.3 | A | 16.0 |
| 9 | 44.5 | 35.9 | A | 8.5 |
| 12 | 34.0 | 50.7 | A | 10.4 |
| 15 | 45.1 | 49.3 | A | 6.1 |
| 18 | 33.7 | 58.0 | A | 16.3 |
| 19 | 43.0 | 45.0 | A | 1.4 |
| 20 | 30.8 | 71.0 | A | 29.2 |
| 22 | 46.8 | 41.8 | A | 5.3 |
| 23 | 37.0 | 39.2 | A | 6.9 |
| 2 | 77.7 | 54.7 | B | 19.7 |
| 7 | 79.9 | 52.5 | B | 22.2 |
| 11 | 65.6 | 68.5 | B | 14.6 |
| 13 | 69.1 | 63.9 | B | 13.6 |
| 16 | 77.3 | 68.2 | B | 22.8 |
| 17 | 80.1 | 58.5 | B | 22.2 |
| 21 | 72.8 | 67.1 | B | 18.5 |
| 24 | 66.9 | 57.4 | B | 9.0 |
| 5 | 58.9 | 36.5 | C | 4.5 |
| 10 | 64.3 | 38.6 | C | 8.4 |
| 14 | 51.1 | 30.1 | C | 11.0 |
Try it in Python
The same 24 people as the Lab, with the stands where they are now, in a few lines of Python. Both run k-Means until no stand moves; Plain Python does every step by hand.
What you are looking at
The graph is a map of a park, 100 metres on each side. Each shape is a person. The lettered rings are ice-cream stands, and everyone walks to the stand nearest to them. Their colour and shape show which one.
Nobody told the model which group anyone belongs to: there are no labels. k-Means finds the groups on its own. The numbers are made up, and Refresh Data shows another day in the park.
What each symbol means
- xᵢ
- Where person i is standing.
- μⱼ
- Where stand j is. (μ is the Greek letter "mu".)
- cᵢ
- The stand person i walks to: the nearest one.
- k
- How many stands, so how many groups.
- J
- Every walk, squared, added together. Smaller is better. The typical walk, √(J ÷ n), turns it back into metres.
How k-Means works
Start with k stands anywhere. Then repeat two moves:
- Group: every person goes to their nearest stand.
- Move: every stand moves to the middle of its people, the average of their positions. The dashed arrows show where each stand will go.
Moving a stand to the middle of its group always shortens that group's walks, and regrouping never makes anyone walk further. So the total J shrinks with every step, until nothing changes: the stands have settled.
Press Step to watch one move at a time, or Run to let it finish. Drag a stand anywhere, and the groups change at once.
Choosing k, and where to start
More stands always mean shorter walks, so J alone cannot pick k: with one stand per person, nobody walks at all. Look instead for the k where adding a stand stops helping much. Here the park has three busy spots, so the typical walk drops a lot from 2 to 3 stands, and only a little from 3 to 4.
Where the stands start matters too. k-Means only ever improves the stands it has, so a bad start can settle on a poor answer, for example one stand stuck between two groups. Drag the stands somewhere new and run it again to see a different result. In practice, k-Means is run from several starts and the run with the smallest J is kept.
The model in short
- Choose k and place k stands.
- Group every point with its nearest stand.
- Move every stand to the middle of its group.
- Repeat until nothing moves: J is then as small as these starting stands allow.