k-Means: Finding Groups Without Answers

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.

Machine Learning · beginner · 10 min · 19 steps · free

What you'll learn

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

  1. Six shoppers: how often each visits.
  2. 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.
  3. Two group centres, the crosses on the chart. Both start in the bottom-left corner, a deliberately poor start.
  4. Round 0. Each round has two moves.
  5. 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).
  6. 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.
  7. 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.)
  8. After round 0 the groups are still wrong, but the centres have spread out.
  9. Round 1.
  10. 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.
  11. Each centre moves to the middle of its new group: x = 1.5 and 7.5.
  12. y = 1.5 and 7.17. Each centre now sits in the middle of its group.
  13. Round 1 found the right groups.
  14. Round 2: will anything change?
  15. Every shopper stays in the same group.
  16. So the centres stay where they are.
  17. No movement at all.
  18. Same groups as the last round.
  19. 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

Best practices

Key takeaways

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.

·