MoonSay /Tutorials/Machine Learning Interview #3
Contents
  1. 01Introduction
  2. 02What clustering is
  3. 03K-means
  4. 04Choosing K, and where K-means fails
  5. 05Gaussian mixture models and the EM algorithm
  6. 06Dirichlet process mixtures
  7. 07DBSCAN
  8. 08Hierarchical clustering
  9. 09Spectral clustering
  10. 10Putting it all together
  11. 11Interview questions and model answers
  12. 12Summary
  13. References
Clustering Algorithms/Chapter 08 of 12

08Hierarchical clustering: a tree of merges

8.1Agglomerative clustering and the dendrogram

Hierarchical clustering does not produce one partition but a whole family of them, nested inside each other. The agglomerative (bottom-up) version starts with every point as its own cluster and repeatedly merges the two closest clusters until one remains, after n-1 merges. The history of merges is drawn as a dendrogram: a tree whose leaves are the points and in which each merge is drawn at a height equal to the distance between the merged clusters. Cutting the tree at any height gives a clustering; cutting where the vertical gaps are longest gives clusters that stay separate over a wide range of scales. In Figure 7(b) the last three merges happen at heights 2.27, 6.65 and 10.49; a cut anywhere between 2.27 and 6.65 gives the three groups of panel (a). No K is needed in advance, and one tree answers the question for every K. Like a family tree, it also shows which clusters are close relatives.

8.2Linkage: what “closest clusters” means

The distance between two clusters G and H must be defined from the distances between their points; this is the linkage (Hastie et al., 2009, Section 14.3.12):

\begin{aligned} \text{single:}\quad & d(G,H)=\min_{x\in G,\,x'\in H}\|x-x'\|, &\qquad \text{complete:}\quad & d(G,H)=\max_{x\in G,\,x'\in H}\|x-x'\|,\\ \text{average:}\quad & d(G,H)=\frac{1}{|G|\,|H|}\sum_{x\in G}\sum_{x'\in H}\|x-x'\|, &\qquad \text{Ward:}\quad & \Delta(G,H)=\frac{|G|\,|H|}{|G|+|H|}\,\|\bar x_G-\bar x_H\|^2 . \end{aligned} \tag{14}

Ward’s criterion (Ward, 1963) merges the pair whose union increases the within-cluster sum of squares (1) the least; by Lemma 1 that increase is exactly \Delta(G,H) in (14) (apply the lemma to G\cup H with z=\bar x_G and with z=\bar x_H). Ward’s method is therefore a greedy, bottom-up version of K-means, and shares its preference for compact, round clusters. The algorithm needs the matrix of all pairwise distances, so it costs O(n^2) memory and, in standard implementations, between O(n^2) and O(n^3) time; it is practical for thousands of points, not millions.

The linkages behave very differently. Single linkage only needs one short link to merge two clusters, so it follows chains of nearby points: it can trace curved clusters, but a few points forming a bridge can also join two clusters that should stay apart (chaining). Complete linkage asks that all points be close, which gives compact clusters of small diameter but can split a large cluster. Average linkage is a compromise, and Ward favours round clusters of similar size. On the two moons of Figure 7(c–f), single linkage recovers both moons perfectly (\mathrm{ARI}=1.00), while complete (0.29), average (0.28) and Ward (0.46) cut them across. Single linkage is in fact equivalent to building the minimum spanning tree of the points and removing its K-1 longest edges, which is also how it is computed fastest.

Worked example 7. Five points on a line, 0,1,5,6,12. The cheapest Ward merges are \{0\}+\{1\} and \{5\}+\{6\}, each costing \frac{1\cdot1}{2}\cdot1^2=0.5. Next, \{0,1\}+\{5,6\} costs \frac{2\cdot2}{4}(0.5-5.5)^2=25, while \{5,6\}+\{12\} would cost \frac{2\cdot1}{3}(5.5-12)^2=28.17, so \{0,1,5,6\} forms first; the final merge with \{12\} costs \frac{4\cdot1}{5}(3-12)^2=64.8. (SciPy draws Ward merges at height \sqrt{2\Delta}, here 1, 1, 7.07 and 11.38.) Single linkage merges at heights 1, 1, 4 and 6, complete linkage at 1, 1, 6 and 12: the same tree here, at different heights.

Hierarchical clustering. (a) Fifteen points coloured by the three clusters obtained by (b) cutting the Ward dendrogram at the dashed line. (c–f) Two clusters from four linkages on two moons: only single linkage follows the curved shapes; ARI against the generating groups. Data: computed for this tutorial; (a, b) with the same data as the companion video.
Figure 7. Hierarchical clustering. (a) Fifteen points coloured by the three clusters obtained by (b) cutting the Ward dendrogram at the dashed line. (c–f) Two clusters from four linkages on two moons: only single linkage follows the curved shapes; ARI against the generating groups. Data: computed for this tutorial; (a, b) with the same data as the companion video.
Try itBuild and cut a dendrogram

The fifteen points of Figure 7(a). Choose a linkage and the agglomerative algorithm runs again; move the cut to choose the number of clusters. Ward heights are drawn as \sqrt{2\Delta}, as in SciPy.

A real case. Eisen et al. (1998) clustered 2{,}467 yeast genes by their expression profiles over a series of experiments (the cell cycle, sporulation, heat shock and others), with a correlation coefficient as the similarity and average linkage. They displayed the data matrix with its rows reordered by the dendrogram, red for induced and green for repressed genes, and genes with related functions appeared as coloured blocks: one block of 125 genes held 112 ribosomal-protein genes. Such heat maps with dendrograms became a standard picture in genomics. Hastie et al. (2009, Section 14.3.8) cluster 64 cancer samples by the expression of 6830 genes; two samples labelled breast cancer were placed in the cluster that held seven of the eight melanomas, and they were later found to be misdiagnosed: they were melanomas that had metastasized. Two cautions from the same source: a hierarchical method imposes a hierarchy “whether or not such structure actually exists in the data”, and the linkage matters (on these data, average and complete linkage agreed, while single linkage produced long, thin clusters).

Pause and think. Is hierarchical clustering with K clusters the same as running K-means with that K?

Show the answer

No. The hierarchical partitions are nested: going from K to K+1 clusters splits exactly one cluster. K-means solutions for different K need not be nested, and Ward’s greedy merges are not guaranteed to minimize J for any K; a common practical trick is to use Ward’s clusters to initialize K-means.