Paul's Python CoursePPC

k-means clustering

Not met yet

k-means sorts points into k groups without being told what the groups are: it learns from the points alone, with no answers. Start with k centres at k different points. Then repeat two steps: every point joins its nearest centre, and every centre moves to the mean of its points. Stop when no point changes group. The inertia, the sum of each point's squared distance to its own centre, can only fall or stay put, so it always stops, though not always in the best place: run it from several starts and keep the lowest. Inertia always falls as k grows, so choose k at the elbow, where one more group stops paying for itself.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks the first k-means step moves each centre straight to the middle of the group it will end up with.
  • Thinks a k-means centre is always one of the points, rather than the mean of the points that chose it.
  • Thinks the k with the lowest inertia is the right number of groups.
  • Thinks inertia rises with more centres, because there are more distances to add up.
  • Thinks k-means always finds the same groups, whatever centres it starts from.
  • Thinks k-means needs labelled examples of each group to learn from.
  • Thinks rng.choice picks different items by default, rather than possibly picking the same one twice.

Where it comes up