ContentsChapter 11 · Clustering1 of 2

11.1

k-means

In this chapter
  1. 11.1k-means
  2. 11.2Gaussian mixtures and EM
Before you readYou have a map of delivery addresses and two vans. If each van takes the addresses nearest to its depot, where would you put the depots?
Watch: K-means clustering · StatQuest with Josh Starmer

Clustering groups data without target labels. In k-means, you choose the number of groups kk Each group has a centre μj\mu_j called a centroid. The aim is to minimise the total squared distance from each point xix_i to its assigned centre μzi\mu_{z_i}

J=∑i∥xi−μzi∥2.J = \sum_i \|x_i - \mu_{z_i}\|^2.

Start with kk centres and alternate two steps:

  1. Assign every point to its nearest centre. With the centres fixed, this gives the smallest squared distance for every point.
  2. Move each centre to the mean of its assigned points. With the groups fixed, the mean minimises the sum of squared distances within its group. An empty group has no mean, so you can leave its centre in place.
-2-1012-4-2024
11 unassigned points
Plus signs mark the centres. Assign points, then move the centres. Do any points switch groups on the next assignment?

For a small example, put points 0,2,3,10,120, 2, 3, 10, 12 on a line and start centres at 00 and 33 At first only 00 joins the left group. The right centre moves to (2+3+10+12)/4=6.75(2+3+10+12)/4 = 6.75 so on the next assignment 22 and 33 switch to the left. Continue until the assignments stop changing.

Neither step increases JJ but the final groups can depend on the starting centres. Try several starts with the same kk and keep the one with the lowest JJ Increasing kk can only lower or leave unchanged the best possible JJ a centre per point gives zero. Look for a point where adding another group helps much less (an elbow), and check that the resulting groups are useful.

Distance depends on your units: changing one feature from metres to centimetres makes its squared differences 10,00010{,}000 times larger. Put features on comparable scales if the units should not decide the groups. Even with sensible scales, k-means favours groups that fit around centres; it gives each point exactly one label, even when two groups overlap.

Recall
What are the two steps of k-means?
Recall
Why rerun k-means from different starting centres?
Exercise
For points 1,3,7,91, 3, 7, 9 start centres at 11 and 66 After one assignment and update, give the new left centre, new right centre, and total squared distance JJ in that order.