Quiz: feature spaces and k-means
Before this unit: Day 12: Thresholding.
See also: Day 33: Random forests; Day 46: Colour; Day 39: Variational autoencoders.
Short questions to test your understanding. Try each on paper first, then open the answer. k-means: assign every point to its nearest centre, move every centre to the mean of its points, repeat; J = \sum_i \lVert\mathbf x_i - \mathbf m_{c(i)}\rVert^2.
Q1. You segment a colour photograph by k-means on the pixels’ (R, G, B) values. What is a “point”, and what is a “cluster” here?
How many numbers describe one pixel?
Every pixel is a point in a 3D space with axes R, G and B; its position in the image plays no role. A cluster is a group of pixels with similar colours, wherever they are in the image. Mapping each pixel’s cluster back to its position gives the segmentation.
Q2. Points 0, 2, 3, 9, centres m = (1, 8). Do one assign step and one update step. What is J before and after the update?
3 is nearer to 1 than to 8.
Assign: \{0, 2, 3\} to 1, \{9\} to 8. J = 1 + 1 + 4 + 1 = 7. Update: m = (5/3, 9) = (1.67, 9). Now J = 2.78 + 0.11 + 1.78 + 0 = 4.67. Assigning again changes nothing: converged.
Q3. Why can k-means never run forever?
What happens to J in each step, and how many different assignments are there?
Neither step can increase J: each point picks the nearest centre, and the mean is the best centre for a fixed group. There are only finitely many ways to assign n points to K groups. If an iteration changes the assignment, it strictly lowers J, so no assignment can come back; after finitely many changes nothing changes any more and the algorithm stops.
Q4. k-means stopped with J = 26 on a data set where you know a solution with J = 1.5 exists. How can that happen, and what do you do?
“Stops” means “no single step helps”.
It stopped in a local minimum: for example two centres share one group while one centre covers two groups; no assign or update step can move a centre across the empty gap. Run again from other starts (preferably k-means++) and keep the run with the lowest J.
Q5. k-means++ has chosen its first centre. Point A is at distance 1 from it, point B at distance 4. How much more likely is B than A to become the second centre?
The probability is proportional to the squared distance.
4^2 / 1^2 = 16 times more likely. Squaring makes far-away points strongly preferred, so the seeds spread over the data.
Q6. Two long, parallel, cigar-shaped groups lie side by side. Sketch what k-means with K = 2 does, and explain why.
Compare J for “cut across both cigars” and “one cigar each”.
It tends to cut across the cigars: one centre near each end, with a straight boundary through the middle of both. Splitting a long group in half reduces the squared distances along its length a lot, more than separating two parallel groups that are close together. k-means only knows round groups; the boundary is always a perpendicular bisector between two centres.
Q7. You cluster nuclei by (area in µm², mean intensity in [0, 1]). The clusters follow the area only. Why, and what should you do?
How large are typical differences in each feature?
Areas differ by tens or hundreds of µm², intensities by a few hundredths. Squared distances are then entirely area differences, so k-means ignores intensity. Rescale the features first (for example divide each by its standard deviation), or choose units in which a “meaningful” difference has a similar size.
Q8. Your colleague picks the K that gives the smallest J. What goes wrong?
What is J with one centre per point?
The best J always decreases as K grows, down to J = 0 at K = n (every point its own centre). The smallest J always asks for the largest K. One needs a trade-off between fit and complexity: an elbow in the J-versus-K curve, or a criterion such as BIC once there is a probability model (unit 3).
Q9. (math) Show that the boundary between the cells of two centres is a straight line (in 2D).
Expand \lVert\mathbf x - \mathbf m_1\rVert^2 = \lVert\mathbf x - \mathbf m_2\rVert^2.
Expanding, \lVert\mathbf x\rVert^2 - 2\mathbf x^\top\mathbf m_1 + \lVert\mathbf m_1\rVert^2 = \lVert\mathbf x\rVert^2 - 2\mathbf x^\top\mathbf m_2 + \lVert\mathbf m_2\rVert^2. The \lVert\mathbf x\rVert^2 terms cancel: 2\mathbf x^\top(\mathbf m_2 - \mathbf m_1) = \lVert\mathbf m_2\rVert^2 - \lVert\mathbf m_1\rVert^2, a linear equation in \mathbf x: a line perpendicular to \mathbf m_2 - \mathbf m_1. The midpoint (\mathbf m_1 + \mathbf m_2)/2 satisfies it.