k-Means

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
ABC

J=∑i=1n∥xi−μci∥2\htmlClass{sym-error}{J} = \sum_{i=1}^{n} \lVert x_i - \mu_{c_i} \rVert^2

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

Typical walk 17.0 m
Press Step or Run

People in a park: 24 people, grouped by stand.
121.458.8A25.4
327.154.2A18.1
426.858.2A20.8
631.314.9A31.0
838.728.3A16.0
944.535.9A8.5
1234.050.7A10.4
1545.149.3A6.1
1833.758.0A16.3
1943.045.0A1.4
2030.871.0A29.2
2246.841.8A5.3
2337.039.2A6.9
277.754.7B19.7
779.952.5B22.2
1165.668.5B14.6
1369.163.9B13.6
1677.368.2B22.8
1780.158.5B22.2
2172.867.1B18.5
2466.957.4B9.0
558.936.5C4.5
1064.338.6C8.4
1451.130.1C11.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:

  1. Group: every person goes to their nearest stand.
    ci=the nearest stand to person ic_i = \text{the nearest stand to person } i
  2. Move: every stand moves to the middle of its people, the average of their positions. The dashed arrows show where each stand will go.
    μj=the average position of the people at stand j\mu_j = \text{the average position of the people at stand } j

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.

J=∑i=1n∥xi−μci∥2\htmlClass{sym-error}{J} = \sum_{i=1}^{n} \lVert x_i - \mu_{c_i} \rVert^2

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

  1. Choose k and place k stands.
  2. Group every point with its nearest stand.
  3. Move every stand to the middle of its group.
  4. Repeat until nothing moves: J is then as small as these starting stands allow.