No labels this time: find two groups of shoppers by repeating two moves. Everyone joins the nearest centre, then each centre moves to the middle of its group.
Explain the two repeating moves of k-means: assign, then update
Watch centres move until nothing changes
Tell unsupervised learning (no answers given) from supervised
kmeans.py
# Six shoppers: how often they visit (x) and how much they spend (y). Find 2 groups.
xs = [1, 2, 1.5, 7, 8, 7.5]
ys = [1, 1.5, 2, 7, 8, 6.5]
cx, cy = [1, 2], [1, 1.5] # two group centres, both starting in the same corner
for step in range(3):
# 1. Each shopper joins the nearest centre.
group = [0 if (x - cx[0]) ** 2 + (y - cy[0]) ** 2 <= (x - cx[1]) ** 2 + (y - cy[1]) ** 2 else 1 for x, y in zip(xs, ys)]
# 2. Each centre moves to the middle of its shoppers.
cx = [sum([x for x, g in zip(xs, group) if g == c]) / group.count(c) for c in (0, 1)]
cy = [sum([y for y, g in zip(ys, group) if g == c]) / group.count(c) for c in (0, 1)]
print("step", step, "groups", group)
Step by step
Six shoppers: how often each visits.
How much each spends per visit. On the chart there are clearly two kinds of shopper: occasional small spenders (bottom left) and frequent big spenders (top right). But nobody told the computer that.
Two group centres, the crosses on the chart. Both start in the bottom-left corner, a deliberately poor start.
Round 0. Each round has two moves.
Move 1: each shopper joins the nearer centre. Only the first shopper, at (1, 1), sits right on centre 0, so it joins group 0. Everyone else, even the far top-right shoppers, is a little closer to centre 1 at (2, 1.5).
Move 2: each centre goes to the middle (average) of its group. Group 1 holds five shoppers, so its centre swings across to x = 5.2.
And up to y = 5. Centre 1 now sits between the two real groups. (It moves across, then up, because x and y are worked out on separate lines.)
After round 0 the groups are still wrong, but the centres have spread out.
Round 1.
Re-assign: now the three bottom-left shoppers are nearer centre 0 and the three top-right ones nearer centre 1. The colours on the chart match the two real kinds of shopper.
Each centre moves to the middle of its new group: x = 1.5 and 7.5.
y = 1.5 and 7.17. Each centre now sits in the middle of its group.
Round 1 found the right groups.
Round 2: will anything change?
Every shopper stays in the same group.
So the centres stay where they are.
No movement at all.
Same groups as the last round.
When a round changes nothing, k-means has converged: two groups, found without ever being told which shopper was which. The same two moves, repeated, work for any number of points and groups.
Common mistakes
Expecting k-means to find the number of groups by itself: you choose k
Forgetting that a bad start can lead to a poor grouping: real code tries several starts
Best practices
Scale features before clustering, since it is based on distance
Key takeaways
k-means finds k groups without any answers (unsupervised learning).
It repeats two moves: everyone joins the nearest centre, then each centre moves to the middle of its group.
It stops when a round changes nothing.
You choose k, and the starting centres can change the result.
Check yourself
A group has points at x = 2, 4 and 9. Where does its centre move (in x)?
Answer: 5. The middle is the average: (2 + 4 + 9) / 3 = 5.
How does k-means know it has finished?
Answer: A round changes nothing. If no point changes group, no centre moves, and every later round would be identical.