Machine Learning Techniques · IIT Madras BS Degree

K-means: what its clusters look like, where to start, how many

Notes for lectures 16 to 18. At convergence every K-means cluster lies in a Voronoi region cut out by perpendicular bisectors between the means, so the boundaries between clusters are straight. The lectures then cover how to start Lloyd's algorithm, from random data points or with K-means++, and how to choose K by adding a penalty on K to the objective.

Part 7 of 18 Instructor Prof. Arun Rajkumar About 60 min of video

At a glance

  • At convergence every point is at least as close to its own mean as to any other. For two means this is the linear condition \(\mathbf{x}^\top(\boldsymbol\mu_2-\boldsymbol\mu_1) \le \tfrac12(\lVert\boldsymbol\mu_2\rVert^2-\lVert\boldsymbol\mu_1\rVert^2)\): the clusters are split by the perpendicular bisector of the two means.
  • With K means, each cluster's region is an intersection of \(K-1\) half-spaces, a Voronoi region. So K-means cannot recover clusters such as two concentric circles; kernel K-means is the fix, outside this course.
  • Lloyd's algorithm converges from any start, but where it ends depends on the start. Common practice: take K random data points as the initial means, run several times, keep the lowest objective.
  • K-means++ picks the initial means one at a time, each data point with probability proportional to its squared distance to the nearest mean already picked. Its expected objective is within a constant factor of the optimum, at the cost of K passes over the data.
  • The objective alone cannot choose K: it is 0 when \(K = n\). Choose K to minimize objective + penalty(K). AIC (\(2K - 2\ln\mathcal{L}\)) and BIC (\(K\ln n - 2\ln\mathcal{L}\)) are standard criteria of this form; the elbow method is another heuristic.

Part 6 showed that Lloyd's algorithm always converges. The next question is what the clusters it converges to look like. Start with the simplest case, \(K = 2\): the algorithm stops with two clusters whose means are \(\boldsymbol\mu_1\) and \(\boldsymbol\mu_2\).

Convergence means every point is happy with its own mean. A point in cluster 1 must be at least as close to \(\boldsymbol\mu_1\) as to \(\boldsymbol\mu_2\), in squared distance. If it were closer to \(\boldsymbol\mu_2\), the next reassignment would move it and the algorithm would not have stopped. So for every \(\mathbf{x}\) in cluster 1:

\[ \lVert \mathbf{x} - \boldsymbol\mu_1 \rVert^2 \;\le\; \lVert \mathbf{x} - \boldsymbol\mu_2 \rVert^2 \]

Expand each side with \(\lVert\mathbf{a}-\mathbf{b}\rVert^2 = \lVert\mathbf{a}\rVert^2 + \lVert\mathbf{b}\rVert^2 - 2\mathbf{a}^\top\mathbf{b}\):

\[ \lVert\mathbf{x}\rVert^2 + \lVert\boldsymbol\mu_1\rVert^2 - 2\,\mathbf{x}^\top\boldsymbol\mu_1 \;\le\; \lVert\mathbf{x}\rVert^2 + \lVert\boldsymbol\mu_2\rVert^2 - 2\,\mathbf{x}^\top\boldsymbol\mu_2 \]

The \(\lVert\mathbf{x}\rVert^2\) terms cancel. Move \(-2\,\mathbf{x}^\top\boldsymbol\mu_2\) to the left, \(\lVert\boldsymbol\mu_1\rVert^2\) to the right, and divide by 2:

Condition for a point of cluster 1 (K = 2) \[ \mathbf{x}^\top(\boldsymbol\mu_2 - \boldsymbol\mu_1) \;\le\; \frac{\lVert\boldsymbol\mu_2\rVert^2 - \lVert\boldsymbol\mu_1\rVert^2}{2} \]

This has the form \(\mathbf{x}^\top\mathbf{w} \le b\) with \(\mathbf{w} = \boldsymbol\mu_2 - \boldsymbol\mu_1\) and \(b = \tfrac12(\lVert\boldsymbol\mu_2\rVert^2 - \lVert\boldsymbol\mu_1\rVert^2)\). It is linear in \(\mathbf{x}\), so we should expect the two clusters to be divided by a line. Points of cluster 2 satisfy the reverse inequality, by the same argument with 1 and 2 swapped.

To see which points satisfy the condition, start from the vector \(\mathbf{w} = \boldsymbol\mu_2 - \boldsymbol\mu_1\), the step you add to \(\boldsymbol\mu_1\) to reach \(\boldsymbol\mu_2\), drawn from the origin.

  1. The points with \(\mathbf{x}^\top(\boldsymbol\mu_2 - \boldsymbol\mu_1) = 0\) have zero dot product with \(\mathbf{w}\). They form the line through the origin at right angles to \(\mathbf{w}\) (line ① below).
  2. Asking instead for \(\mathbf{x}^\top(\boldsymbol\mu_2 - \boldsymbol\mu_1) = b\) moves that line parallel to itself, along \(\mathbf{w}\). In the lecture's picture \(\lVert\boldsymbol\mu_2\rVert\) is larger than \(\lVert\boldsymbol\mu_1\rVert\), so \(b > 0\) and the line moves toward \(\boldsymbol\mu_2\) (line ②). Whether \(b\) is positive or negative depends on which mean is longer.
  3. Cluster 1 is the side of line ② where \(\mathbf{x}^\top\mathbf{w} \le b\), the side that contains \(\boldsymbol\mu_1\). Cluster 2 is the other side.

How far did the line move? Find one point on it. The average of the two means works:

\[ \Big(\frac{\boldsymbol\mu_1 + \boldsymbol\mu_2}{2}\Big)^{\!\top}(\boldsymbol\mu_2 - \boldsymbol\mu_1) = \frac{\boldsymbol\mu_1^\top\boldsymbol\mu_2 - \lVert\boldsymbol\mu_1\rVert^2 + \lVert\boldsymbol\mu_2\rVert^2 - \boldsymbol\mu_2^\top\boldsymbol\mu_1}{2} = \frac{\lVert\boldsymbol\mu_2\rVert^2 - \lVert\boldsymbol\mu_1\rVert^2}{2} \]

The cross terms cancel because \(\boldsymbol\mu_1^\top\boldsymbol\mu_2 = \boldsymbol\mu_2^\top\boldsymbol\mu_1\). So line ② passes through the midpoint of the two means and is at right angles to the segment joining them: it is the perpendicular bisector of that segment. This is also the direct way to see it. Join the two means, draw the perpendicular bisector, and every point on one side is closer to the mean on that side.

origin shift μ₂ − μ₁ μ₁ μ₂ midpoint (4, 4) ① ② cluster 1 side cluster 2 side Closer to μ₁ than to μ₂ ‖x − μ₁‖² ≤ ‖x − μ₂‖² ⇔ xᵀ(μ₂ − μ₁) ≤ b with b = (‖μ₂‖² − ‖μ₁‖²) / 2 In this picture μ₁ = (2, 3), μ₂ = (6, 5) μ₂ − μ₁ = (4, 2), b = (61 − 13)/2 = 24 ① 4x + 2y = 0: through the origin, at right angles to μ₂ − μ₁ ② 4x + 2y = 24: line ① moved parallel, along μ₂ − μ₁ The midpoint is on ②: 4·4 + 2·4 = 24. So ② is the perpendicular bisector of the segment from μ₁ to μ₂.
Two means, one straight boundary. Line ① is \(\mathbf{x}^\top(\boldsymbol\mu_2 - \boldsymbol\mu_1) = 0\). Moving it along \(\boldsymbol\mu_2 - \boldsymbol\mu_1\) until the right-hand side is \(b\) gives line ②, which passes through the midpoint at a right angle to the segment. Every point on the shaded side is closer to \(\boldsymbol\mu_1\). The means are illustrative numbers.

With K = 2, the converged K-means clusters are always separated by a straight line (a hyperplane in \(\mathbb{R}^d\)). This follows from the convergence condition alone.

With \(K = 3\) the algorithm converges with three means \(\boldsymbol\mu_1, \boldsymbol\mu_2, \boldsymbol\mu_3\). The data is drawn in two dimensions to make it visible; nothing in the argument depends on the dimension.

A point in cluster 1 must be closer to \(\boldsymbol\mu_1\) than to \(\boldsymbol\mu_2\) and closer to \(\boldsymbol\mu_1\) than to \(\boldsymbol\mu_3\). If either failed, the point would jump to the other cluster and the algorithm would not have converged.

  • Closer to \(\boldsymbol\mu_1\) than to \(\boldsymbol\mu_2\): the \(\boldsymbol\mu_1\) side of the perpendicular bisector of \(\boldsymbol\mu_1\) and \(\boldsymbol\mu_2\).
  • Closer to \(\boldsymbol\mu_1\) than to \(\boldsymbol\mu_3\): the \(\boldsymbol\mu_1\) side of the bisector of \(\boldsymbol\mu_1\) and \(\boldsymbol\mu_3\). Since \(\boldsymbol\mu_3\) need not lie on the line through \(\boldsymbol\mu_1\) and \(\boldsymbol\mu_2\), this is a different line.
  • Cluster 1's region is the intersection of the two sides.

The bisector of \(\boldsymbol\mu_2\) and \(\boldsymbol\mu_3\) then splits what is left: the region of \(\boldsymbol\mu_2\) (closer to \(\boldsymbol\mu_2\) than to both others) and the region of \(\boldsymbol\mu_3\). There are three separators, one for each pair of means.

Region of μ₁: two half-planes μ₁ μ₂ μ₃ All three regions μ₁ μ₂ μ₃ μ₁ | μ₂ μ₁ | μ₃ μ₁ side μ₁ side μ₁ | μ₂ μ₁ | μ₃ μ₂ | μ₃ shaded: points closer to μ₁ than to both μ₂ and μ₃ solid: region boundaries · faint dashes: unused parts
Three means, three bisectors. Left: the region of \(\boldsymbol\mu_1\) is where the \(\boldsymbol\mu_1\) sides of two bisectors overlap. Right: the third bisector divides the rest between \(\boldsymbol\mu_2\) and \(\boldsymbol\mu_3\). Each region uses only the part of a bisector that borders it; the dashed parts are not boundaries. The means are illustrative.

One side of a line is a half-space: the line divides the plane in two, and each half is a half-space. In higher dimensions a hyperplane plays the role of the line. Every mean is compared with the \(K - 1\) other means, so:

The region of mean k \[ R_k = \{\mathbf{x} : \lVert\mathbf{x}-\boldsymbol\mu_k\rVert^2 \le \lVert\mathbf{x}-\boldsymbol\mu_j\rVert^2 \ \text{for all } j \ne k\} = \bigcap_{j \ne k} \Big\{ \mathbf{x} : \mathbf{x}^\top(\boldsymbol\mu_j - \boldsymbol\mu_k) \le \frac{\lVert\boldsymbol\mu_j\rVert^2 - \lVert\boldsymbol\mu_k\rVert^2}{2} \Big\} \]

Each cluster region is an intersection of \(K - 1\) half-spaces, and the K regions divide the whole space (\(\mathbb{R}^2\) here, \(\mathbb{R}^d\) in general) between them.

L16 · 17:13

Voronoi regions

Regions built this way, one per centre, each holding the points closer to that centre than to any other, are called Voronoi regions, and together they form a Voronoi partition of the space. The argument above holds in any dimension. So the clusters produced by Lloyd's algorithm always sit in the Voronoi regions of their own means, and Lloyd's algorithm can be read as a search for a good Voronoi partition: one whose clusters have a small objective.

Try it below. Drag the means and the regions follow. One Lloyd step moves every mean to the average of the points in its region, the update from Part 6; the points then fall into the regions of the new means.

3
Objective F (nearest mean)–
Lloyd steps–
Voronoi regions of the current means, on synthetic data (64 points in three blobs) generated for this page. Each shaded region holds the points closer to its mean than to any other, and its edges are pieces of perpendicular bisectors. Drag a mean, or focus it and use the arrow keys. Random start picks K data points as the means, the initialization of Lecture 17. Keep stepping until the means stop moving: then each mean is the average of the points in its own region, the converged state analysed above.

Where Voronoi clusters fail

The geometry also tells us when K-means will fail. Take two-dimensional data on two concentric circles: a ring of points around a smaller circle of points. The natural clustering has two clusters, the outer ring and the inner circle.

Run Lloyd's algorithm with \(K = 2\). It converges, as always. But the two clusters must lie in Voronoi regions, so a line separates them. Exactly where the line falls depends on how the points are spaced (it need not be slanted; it could be vertical), but it is a line, and no line can put the whole ring on one side and the inner circle on the other, because the ring surrounds the inner circle. K-means as described can never return the inner/outer split.

What we want What K-means (K = 2) can give μ₁ μ₂ filled: inner circle · hollow: outer ring filled: cluster 1 · hollow: cluster 2 · line: boundary
Concentric circles defeat K-means. Illustrative points: 8 on an inner circle and 24 on an outer ring. The right panel is an actual converged K = 2 result on these points, from one starting pair. The boundary is a line, so each cluster is half ring and half inner circle. A different start or spacing moves the line, but it stays a line.

If you do not expect your clusters to sit in Voronoi-like regions, expect K-means to fail on that data.

Standard fact, not from the lecture: an intersection of half-spaces is convex, so every K-means cluster region is convex. A ring is not convex, so no choice of K can return the ring as one cluster.

The fix, in outline L16 · 21:11

The course does not develop this, but the lecture gives pointers. Recall Part 5: when features are related non-linearly, map the data to a higher-dimensional space, find linear structure there, and it becomes non-linear structure in the original space. The same idea applies to clustering. The data may have no natural Voronoi structure in its own space, yet after a suitable map to a high-dimensional space its natural clusters may fall into different Voronoi regions, where plain Lloyd's algorithm works. The kernel trick avoids the cost of computing that map. The result is called kernel K-means.

Kernel K-means is closely related to computing eigenvectors of the kernel matrix and using them as a new representation of the data points, which are then clustered. It is also related to a popular method called spectral clustering, which in turn has much in common with PCA. Neither is covered in this course. 21:45

Two issues about Lloyd's algorithm remain: how to initialize it, and how to choose the number of clusters. 22:49

The convergence argument in Part 6 never used the starting point, so Lloyd's algorithm converges however it is initialized. Where it converges does depend on the start, because it can get stuck in a local minimum. A good initialization gives it a better chance of ending in good clusters. The lecture discusses these options:

  • Random boxes. Put each data point into one of the K clusters uniformly at random. The algorithm still converges, but this ignores the structure of the data and gives no initial push to escape poor local minima.
  • Random data points as means. Pick K data points uniformly at random and treat them as the initial means. This initializes the means rather than the cluster indicators. Every other point then joins its closest mean, so at least the start has the property we want at the end: each point sits with its nearest mean. With 1000 points and K = 5, the means might be data points number 5, 72, 89, 22 and one more; each of the other 995 points goes to the nearest of these five. This is not yet a converged state: the means are recomputed from the new clusters and Lloyd's algorithm carries on. In practice this is what is typically done.
  • Restarts. The K random points may be an unlucky pick. So repeat the whole run with fresh random picks several times and keep the clustering with the best (lowest) objective value.
  1. Random initialization with restarts
  2. Repeat several times:
  3. Pick K data points uniformly at random as the initial means.
  4. Assign every point to its nearest mean, then run Lloyd's algorithm to convergence.
  5. Record the final partition and its objective \(\sum_{i=1}^n \lVert\mathbf{x}_i - \boldsymbol\mu_{z_i}\rVert^2\).
  6. Return the partition with the smallest objective.
Start 1: two seeds in one blob converged: F = 84.8 Start 2: one seed per blob converged: F = 23.5 rings: starting means (data points) · yellow: converged means · marker style: final cluster
Same data, two random starts. Synthetic data (42 points in three blobs, two of them close together) generated for this page. Both starts use data points as the initial means. Start 1 takes two from the same far blob; Lloyd's algorithm converges with that blob split in two and the two near blobs merged, objective 84.8. Start 2 converges to the natural clusters, objective 23.5. Restarting and keeping the lower objective returns start 2.

A more principled option is K-means++, which is the usual K-means (Lloyd's) algorithm with a better initialization. It still starts from K data points, but picks them with care.

What should good initial means look like? At convergence the means represent the clusters: the whole data set is compressed into K representatives. Representatives should be as different from each other as possible. If two of them were close, two clusters would describe nearly the same thing, and one cluster would do. So K-means++ starts with means that are far apart, with a probabilistic twist, and it picks them one at a time instead of all at once.

  1. K-means++ initialization (superscript 0 marks the initial means)
  2. Pick the first mean \(\boldsymbol\mu^0_1\) uniformly at random from \(\{\mathbf{x}_1, \dots, \mathbf{x}_n\}\).
  3. For \(l = 2, \dots, K\):
    • Give every data point the score \(s(\mathbf{x}) = \min_{j = 1, \dots, l-1} \lVert \mathbf{x} - \boldsymbol\mu^0_j \rVert^2\), its squared distance to the closest mean picked so far.
    • Pick \(\boldsymbol\mu^0_l\) at random from the data points, choosing \(\mathbf{x}_i\) with probability \(s(\mathbf{x}_i) \,/\, \sum_{m=1}^n s(\mathbf{x}_m)\).
  4. Run Lloyd's algorithm starting from \(\boldsymbol\mu^0_1, \dots, \boldsymbol\mu^0_K\).

Why the minimum? The distance to the closest chosen mean measures how far a point is from all of them: if even the closest one is far away, every chosen mean is far away. A point that has already been picked has score 0, so it cannot be picked twice. Standard name: sampling in proportion to this squared distance is called D² sampling.

Sampling in proportion to a score L17 · 8:50

Scores become probabilities by dividing each by their total. Suppose only three points remain, with scores 10, 20 and 30. The total is 60, so:

PointScore s(x)Probability
\(\mathbf{x}_1\)1010/60 = 1/6
\(\mathbf{x}_2\)2020/60 = 1/3
\(\mathbf{x}_3\)3030/60 = 1/2

With 1000 points and 5 means already picked, the other 995 points are the candidates for the sixth mean, and the same normalization turns their 995 scores into a probability distribution to sample from.

4
Means picked–
Sum of scores Σ s(x)–
Most likely next pick–
K-means++ picking its initial means, on synthetic data (40 points in four blobs) generated for this page. A halo's area is proportional to the chance that point is picked next: its score (squared length of its thin grey line to the nearest picked mean) over the sum of all scores. The ringed point is the most likely next pick, but any point with a halo can be picked. Hover a point for its score. Once K means are picked, the sum of scores is the objective of assigning each point to its nearest initial mean.

Why sample instead of taking the farthest point?

Why not simply take the point with the largest score, the one whose nearest chosen mean is farthest away? In the example that rule always picks \(\mathbf{x}_3\). Under K-means++, \(\mathbf{x}_3\) is still the most likely pick (probability 1/2), but \(\mathbf{x}_2\) or even \(\mathbf{x}_1\) can be chosen.

The randomness is what makes a guarantee possible. For any deterministic rule one can build an adversarial data set on which the guarantee fails. A randomized rule can instead promise something on average. K-means++ does the natural thing: the point the deterministic rule would choose gets the highest probability, and every other point still gets some.

Run K-means++ and then Lloyd's algorithm. Once the initial means are fixed, Lloyd's algorithm is deterministic. The initial means are random, though, so the final objective value is a random quantity. The guarantee compares its expected value, over the random initialization, with the best objective any partition can achieve.

K-means++ guarantee (stated, not proved, in the course) \[ \mathbb{E}\Big[\sum_{i=1}^n \lVert\mathbf{x}_i - \boldsymbol\mu_{z_i}\rVert^2\Big] \;\le\; c \cdot \min_{z_1, \dots, z_n} \sum_{i=1}^n \lVert\mathbf{x}_i - \boldsymbol\mu_{z_i}\rVert^2 \]

Here \(c\) is a constant factor that does not depend on the data. Finding the minimizing partition on the right is NP-hard, so we cannot expect to reach it. K-means++ promises that on average it will not be too far away. For example, if the best possible objective is 10, the average K-means++ objective might be at most \(5 \times 10 = 50\): worse than the best, but not by much. 16:32

Standard result, beyond the lecture (Arthur and Vassilvitskii, 2007): the factor is \(c = 8(\ln K + 2)\), so it grows only slowly with K. It already holds for the initial means alone, and Lloyd's iterations can only lower the objective from there.

The cost L17 · 17:07

The price is time. Each of the K rounds computes a score for every data point, normalizes the scores and samples. With a billion points and 10 means, that is 10 passes over a billion points before Lloyd's algorithm even starts. There are tricks to speed this up, but the K rounds remain.

Picking K points uniformly at random, and trying several times, is faster, but it is not principled: nothing pushes the means apart. Even so, in practice, especially with high-dimensional data, the uniform method is used extensively. The choice depends on how much time and computing you have. K-means++ is a solid way to initialize when you can afford it. 17:41

InitializationHow the start is chosenCostGuarantee
Random boxesEach point to a random clusterCheapNone; ignores the data
Random data points + restartsK points uniformly at random; repeat, keep the lowest objectiveCheap per runNone, but widely used
K-means++One at a time, in proportion to squared distance to the nearest chosen meanK passes over the dataExpected objective \(\le c \times\) optimum

The objective alone cannot pick K

In practice nobody gives you K. You get data points, and usually you do not know how many natural clusters they contain, so K has to be chosen from the data. The lecture gives broad principles. The specific methods are all heuristics, there are more of them than one lecture can cover, and some are worked through in tutorial sessions.

Could the objective itself choose K? Recall

\[ F(z_1, \dots, z_n) = \sum_{i=1}^n \lVert \mathbf{x}_i - \boldsymbol\mu_{z_i} \rVert^2 \]

Take \(K = n\). Every data point is its own cluster and its own mean, every term is 0, and \(F = 0\), the smallest value \(F\) can take. If K were chosen by minimizing \(F\) as well, \(K = n\) would always win. That is useless: a separate cluster for every point is hardly a clustering, and it compresses nothing.

So two goals pull in opposite directions. For a fixed K we still want the partition that minimizes \(F\), or gets as close as we can. But we also want K small, because the point is to understand the data in compressed form. A larger K always allows a lower objective, so the objective alone cannot decide K.

For each candidate K, measure two things: the objective of the partition the algorithm returns, and how big K is. Then penalize large K:

\[ K^* = \arg\min_{K} \;\Big[\, F_K + \text{penalty}(K) \,\Big] \]

\(F_K\) is the objective of the best partition found with K clusters, and \(\text{penalty}(K)\) is an increasing function of K that we choose.

Think of the penalty as the price of buying one more cluster. Going from \(K = 5\) to \(K = 6\) costs \(\text{penalty}(6) - \text{penalty}(5)\). With more clusters, clusters typically get smaller and points get closer to their means, so the objective drops. The purchase is worth it only if it drops by more than the price. From \(K = 1\) to \(K = 2\) the drop is usually large, since with one cluster nothing is really being clustered. As K grows, the price keeps rising while the extra drop shrinks, and somewhere the two balance.

0 100 200 300 400 500 600 700 800 900 1000 1 2 3 4 5 6 7 8 9 10 number of clusters K value K* = 4 objective F (best run) penalty 45 · K objective + penalty The 60 points
Objective plus penalty picks K. Synthetic data (60 points in four blobs) generated for this page. For each K the objective is the best of 25 K-means++ runs. With the illustrative penalty \(45K\), the sum is smallest at \(K^* = 4\). The objective keeps falling and would reach 0 at \(K = n = 60\). K takes whole values only; the lines just show the trend.

This is the lecture's sketch: against \(K = 1, 2, \dots, n\), the objective falls and reaches 0 at \(K = n\), the penalty rises, and their sum falls at first until the penalty takes over and it starts to rise. The K at the bottom is the choice.

  1. Choosing K by objective + penalty
  2. For each candidate K, run Lloyd's algorithm. Any initialization works: K-means++, or uniform random starts run several times.
  3. Keep the best partition for that K and its objective \(F_K\).
  4. Compute \(F_K + \text{penalty}(K)\).
  5. Choose the \(K^*\) with the smallest sum.

What the procedure leaves open is the penalty function itself. That is where specific criteria come in.

Two common criteria of the form "objective plus penalty":

CriterionMinimizePenalty grows like
AIC, Akaike information criterion\(2K - 2\ln\mathcal{L}\)K
BIC, Bayesian information criterion\(K\ln n - 2\ln\mathcal{L}\)K times \(\ln n\)

Here \(\mathcal{L}\) is a likelihood: roughly, how probable the observed data is under parameters estimated from that data. The term \(-2\ln\mathcal{L}\) plays the part of the objective, measuring how well the model fits, and the other term is the penalty on K. Both penalties grow linearly in K. BIC's also grows with the number of points, through \(\ln n\).

Each criterion is optimal under a different assumption about how the data is generated probabilistically. With such an assumption you can measure fit in a more nuanced way than the K-means objective, through the likelihood. The course has not yet modelled data probabilistically, so the lecture leaves the details for later. Estimation starts in Part 8, and the lecture plans to revisit model selection once that is in place.

Standard definitions: the K in AIC and BIC counts the free parameters of the probabilistic model. For a clustering model that number grows with the number of clusters.

These criteria are not set in stone. The elbow method and other choices are also common in practice. The lecture only names the elbow method. Standard description: plot the objective against K and pick the K where the curve bends from steep to flat. In the figure above that is \(K = 4\).

Run the algorithm for several values of K, then pick K with a criterion that balances the objective against a penalty on K.

Lloyd's algorithm: four questions, answered

That completes the discussion of Lloyd's algorithm. The four questions posed about it in Part 6 now all have answers:

QuestionAnswerWhere
Does it converge?Yes, though possibly to a local optimum.Part 6
What clusters does it produce?Voronoi regions. With the kernel trick, Voronoi regions in a high-dimensional space.L16
How should it be initialized?K-means++, which comes with a guarantee on the expected objective (or random data points with restarts).L17
How do we choose K?Minimize objective + penalty(K).L18

This ends clustering. So far the course has been about unsupervised learning: representation learning (PCA and kernel PCA) and clustering, with K-means as the main example. The next topic, estimation, continues with unsupervised learning, but it will later apply to supervised learning too, so it acts as a bridge between the two.

Key terms

Convergence condition
At convergence every point is at least as close to its own mean as to any other mean.
Perpendicular bisector
The line (hyperplane) through the midpoint of two means at right angles to the segment between them; the K = 2 boundary.
Half-space
One side of a line in \(\mathbb{R}^2\), or of a hyperplane in \(\mathbb{R}^d\): \(\{\mathbf{x} : \mathbf{x}^\top\mathbf{w} \le b\}\).
Voronoi region
The set of points closer to one centre than to every other; an intersection of \(K-1\) half-spaces. K-means clusters lie in them.
Kernel K-means
K-means run in a high-dimensional feature space via the kernel trick, so clusters can be non-linear in the original space.
Local minimum
A partition Lloyd's algorithm cannot improve on by its own steps, though better partitions exist.
Random initialization
Start from K data points picked uniformly at random as the means.
Restarts
Run from several random starts and keep the partition with the lowest objective.
K-means++
Lloyd's algorithm with initial means picked one at a time by D² sampling.
Score / D² sampling
\(s(\mathbf{x}) = \min_j \lVert\mathbf{x} - \boldsymbol\mu^0_j\rVert^2\); the next mean is \(\mathbf{x}_i\) with probability \(s(\mathbf{x}_i)/\sum_m s(\mathbf{x}_m)\).
Penalty on K
An increasing function of K added to the objective so that extra clusters must pay for themselves.
AIC
Akaike information criterion, \(2K - 2\ln\mathcal{L}\): penalty linear in K.
BIC
Bayesian information criterion, \(K\ln n - 2\ln\mathcal{L}\): penalty grows with K and with \(\ln n\).
Elbow method
Pick the K where the objective-versus-K curve bends from steep to flat.

Test yourself

Q1K-means with K = 2 has converged with \(\boldsymbol\mu_1 = (0, 0)\) and \(\boldsymbol\mu_2 = (4, 0)\). Which points can belong to cluster 1?

\(\mathbf{x}^\top(\boldsymbol\mu_2 - \boldsymbol\mu_1) \le \tfrac12(16 - 0)\), that is \(4x \le 8\), or \(x \le 2\). The boundary is the vertical line \(x = 2\), the perpendicular bisector of the two means.

Q2Write the boundary between \(\boldsymbol\mu_1 = (1, 1)\) and \(\boldsymbol\mu_2 = (3, 5)\), and check that the midpoint lies on it.

\(\mathbf{w} = (2, 4)\) and \(b = \tfrac12(9 + 25 - 1 - 1) = 16\), so the boundary is \(2x + 4y = 16\), or \(x + 2y = 8\). The midpoint \((2, 3)\) gives \(2 + 6 = 8\).

Q3With K = 4, how many half-spaces cut out each cluster's region, and how many pairwise bisectors are there?

Each region is an intersection of \(K - 1 = 3\) half-spaces, one for each other mean. There is one bisector per pair of means: \(4 \times 3 / 2 = 6\).

Q4Why can't K-means with K = 2 put an inner circle of points in one cluster and the surrounding ring in the other?

The converged clusters lie in Voronoi regions, so with K = 2 they are separated by a straight line. No line can have the whole ring on one side and the inner circle on the other, because the ring surrounds the inner circle.

Q5One-dimensional data \(0, 1, 2, 10\). K-means++ has picked \(\mu^0_1 = 0\). What is the probability of each point becoming \(\mu^0_2\)?

Scores are squared distances to 0: \(0, 1, 4, 100\), total 105. Probabilities: \(0\), \(1/105 \approx 0.010\), \(4/105 \approx 0.038\), \(100/105 \approx 0.952\). The far point is very likely, but not certain.

Q6Why does K-means++ sample instead of always taking the point farthest from the chosen means?

Its guarantee is about the expected objective. For any deterministic rule, an adversarial data set can be built on which the guarantee fails. Randomizing, while still giving the farthest point the highest probability, allows a guarantee on average.

Q7In the objective-plus-penalty figure, \(F_3 = 232.8\), \(F_4 = 87.0\), \(F_5 = 69.8\) and the penalty is \(45K\). Is the fourth cluster worth buying? The fifth?

The fourth costs 45 and lowers the objective by 145.8, so yes. The fifth costs 45 but lowers it by only 17.2, so no, and every later cluster lowers it by even less. So \(K^* = 4\).

Q8For \(n = 100\) points, which penalizes each extra unit of K more, AIC or BIC?

AIC adds 2 per unit of K; BIC adds \(\ln 100 \approx 4.6\). BIC penalizes more whenever \(\ln n > 2\), that is for \(n \ge 8\).