MoonSay /Tutorials/Machine Learning Interview #1
Contents
  1. 01Introduction
  2. 02The learning signal
  3. 03Supervised learning
  4. 04Clustering
  5. 05Dimensionality reduction
  6. 06Semi-supervised learning
  7. 07Label propagation
  8. 08Self-training, consistency and FixMatch
  9. 09Self-supervised learning
  10. 10Putting it all together
  11. 11Interview questions and model answers
  12. 12Summary
  13. References

04Clustering: K-means and Lloyd’s algorithm

Now remove the answer key. We only have inputs x_1,\dots,x_n, and want to find groups of similar points. This is clustering, the most familiar unsupervised task.

4.1Intuition: where to put the post offices

Imagine a town planner who must open K post offices so that people walk as little as possible. Each resident will use the nearest office, and the planner measures the cost as the sum of squared walking distances. Two simple moves suggest themselves. If the offices are fixed, every resident should go to the nearest one. If the groups of residents are fixed, each office should move to the centre of its group, where the squared distances are smallest. Alternating these two moves is exactly the K-means algorithm. The same problem appears when choosing K colours to repaint a photograph with little visible change (colour quantization) or K T-shirt sizes that fit most customers.

4.2The objective

Give each point x_i a cluster label c_i\in\{1,\dots,K\} and each cluster a centre \mu_k\in\mathbb{R}^d. K-means minimizes the within-cluster sum of squares

J(c,\mu)=\sum_{i=1}^{n}\big\|x_i-\mu_{c_i}\big\|^2=\sum_{k=1}^{K}\ \sum_{i:\,c_i=k}\big\|x_i-\mu_k\big\|^2 . \tag{6}

Read as a compression problem, J is the total squared error made when every point is replaced by the centre of its cluster. This is the sense in which an unsupervised method still has a target, the input itself (Table 1).

4.3Lloyd’s algorithm and why it converges

Minimizing J over both c and \mu at once is hard; in fact finding the global minimum is NP-hard even for K=2 (Aloise et al., 2009). Minimizing over one while the other is fixed is easy:

  1. Assignment step. With the centres fixed, J is a sum of separate terms, one per point, so each c_i is chosen on its own: c_i=\arg\min_k\|x_i-\mu_k\|^2, the nearest centre.
  2. Update step. With the assignments fixed, J is a sum of separate terms, one per cluster. Setting the gradient of \sum_{i:c_i=k}\|x_i-\mu_k\|^2 to zero gives -2\sum_{i:c_i=k}(x_i-\mu_k)=0, that is

    \mu_k=\frac{1}{n_k}\sum_{i:\,c_i=k}x_i,\qquad n_k=\#\{i:c_i=k\}, \tag{7}

    the mean of the points in cluster k. (This is why the method is called K-means.)

Start from some initial centres and repeat the two steps until the assignments stop changing.

Theorem 1 (Convergence of Lloyd’s algorithm). No step of Lloyd’s algorithm increases J, and the algorithm stops after finitely many iterations.

The assignment step minimizes J exactly over c with \mu fixed, and the update step minimizes J exactly over \mu with c fixed, so neither can increase J. Each iteration that changes the assignments strictly lowers J (if it did not, the old assignment would have been as good, and ties can be broken consistently). There are at most K^n ways to assign n points to K clusters, and since J strictly decreases no assignment can repeat. Hence the algorithm must stop after finitely many iterations.

The theorem guarantees a local minimum, not the global one: a bad start can leave two centres splitting one group while another covers two. The remedies are several random starts, keeping the smallest J, and careful seeding: k-means++ picks the first centre at random and each next one with probability proportional to its squared distance from the nearest centre already chosen, which spreads the centres out and guarantees an expected J within a factor O(\log K) of the optimum (Arthur and Vassilvitskii, 2007).

Worked example 2. Six points on a line, 1,2,3,8,9,10, and K=2 with a poor start, \mu=(1,2). Assign: only the point 1 is closest to \mu_1, so the clusters are \{1\} and \{2,3,8,9,10\} and J=0+(0+1+36+49+64)=150. Update: \mu=(1,\,6.4), and J drops to 53.2. Assign: the point 3 is now closer to 1 (distance 2) than to 6.4 (distance 3.4), so the clusters become \{1,2,3\} and \{8,9,10\} and J=27.28. Update: \mu=(2,9) and J=4. The next assignment changes nothing, so the algorithm stops. The sequence 150\to53.2\to27.28\to4 never increases, as Theorem 1 promises.

The video’s animation. Figure 3 reproduces the K-means animation of the video: 135 points from three groups of 45 and three centres started far from the data. After the first assignment J=1327.9; after the first update 463.4; the subsequent half-steps give 427.1, 350.7, 188.7, 105.3, 91.1, 89.4, 88.9 and 88.7. After five updates the sixth assignment changes nothing, and the three clusters found are exactly the three groups of 45 points that generated the data.

Lloyd’s algorithm on 135 points (K=3). (a) The starting centres. (b) First assignment. (c) First update: each centre moves to the mean of its points (arrows). (d) After convergence (five updates). J is the objective (6). Data: computed for this tutorial, with the same data and starting centres as the companion video.
Figure 3. Lloyd’s algorithm on 135 points (K=3). (a) The starting centres. (b) First assignment. (c) First update: each centre moves to the mean of its points (arrows). (d) After convergence (five updates). J is the objective (6). Data: computed for this tutorial, with the same data and starting centres as the companion video.
Try itRun Lloyd's algorithm step by step

The 135 points of Figure 3, with the same starting centres for K=3. Alternate the two steps and watch the objective J=\sum_i\|x_i-\mu_{c_i}\|^2: it never goes up. Try a random start, or a different K.

4.4Choosing K, and what K-means assumes

The objective cannot choose K by itself: J always decreases as K grows, reaching zero when every point is its own cluster. Two common heuristics are the elbow of the curve of J against K, where adding a cluster stops paying off, and the silhouette (Rousseeuw, 1987). For point i, let a(i) be its mean distance to the other points of its own cluster and b(i) its mean distance to the points of the nearest other cluster; then s(i)=\big(b(i)-a(i)\big)/\max\{a(i),b(i)\} lies between -1 and 1, and a K with a high average silhouette gives compact, well-separated clusters. Often the application decides: a marketing team may want four segments because it can run four campaigns.

Because it uses squared distance to a single centre, K-means favours round clusters of similar size, and it is sensitive to feature units (standardize first) and to outliers. A Gaussian mixture model relaxes this with a mean, covariance and weight per cluster and soft assignments fitted by EM (Dempster et al., 1977); K-means is its limit when all components share the covariance s^2I and s\to0 (Bishop, 2006, Section 9.3.2). DBSCAN (Ester et al., 1996) defines clusters as dense regions, finds any shape, needs no K and marks isolated points as noise. Clustering is treated in depth, with the mathematics of each method, in the Episode 3 tutorial.

Pause and think. If K-means used absolute distance \|x_i-\mu_k\|_1 instead of squared distance, what would the update step compute?

Show the answer

The coordinate-wise median of the cluster instead of the mean, because the median minimizes a sum of absolute deviations. The algorithm is called K-medians and is less sensitive to outliers.