Machine Learning · Insper

Hierarchical Clustering

Advanced approach

Which linkage finds which shapes, which distance suits which data, how far it scales, and when a tree is the right tool

The path of this part

  1. Match the linkage to the shapechaining against compact groups; a local rule for the cut
  2. Choose the distanceone update formula; correlation and cosine
  3. Face the costquadratic memory and time; sample, then assign
  4. Choose the toolagainst k-means and density-based clustering; applications
Each stage answers one question: which linkage finds which shapes? → which distance for which data? → how far does it scale? → when is a tree the right tool?
Stage 1 · which linkage finds which shapes?

Linkage behaviors, measured

Four datasets with known groups, 300 points each, every method told the true number of clusters. Score: the ARI (Adjusted Rand Index): 1 for the true groups, about 0 for chance.

Predict. Which linkage recovers the two moons?
singlecompleteaverageWard
blobs0.570.980.980.98
blobs + bridge0.000.910.990.96
moons1.000.200.430.30
parallel strips1.000.120.190.22

Two camps

Four datasets by four linkages: single wins on moons and strips but fails on blobs and the bridge; the others the opposite

Single asks "is there some short hop?": it follows chains (1.00 on moons and strips), and a bridge of 20 points merges two blobs (0.00). That is chaining.

Complete, average, Ward compare the groups as a whole: a bridge cannot pull blobs together, but they cut moons and strips across, as k-means does.

Ward (the default) or average for compact groups; single for thin, connected, well-separated shapes. Always look at the tree.

A local rule for the cut: the inconsistency coefficient

\(h\) = height of a merge; \(\bar h\), \(s\) = mean and standard deviation of \(h\) and the heights of the merges just below it (depth 2: at most three heights). Ward tree of the 400 points.

\[ \text{coefficient} = \frac{h - \bar h}{s} \]
merge\(\bar h\)\(s\)coef.
4th from top7.053.050.86
3rd16.1412.221.15
2nd16.9013.361.15
top73.9373.871.15
Ward tree of 400 points with cuts at 2 and 4 clusters; gap 126.9 for k=2 and 20.6 for k=4
Three heights: the coefficient cannot exceed \(2/\sqrt3 = 1.15\), and the top three merges all reach it. It cannot tell \(k = 2\) from \(k = 4\); the gaps can.
Stage 2 · which distance for which data?

One update formula for every linkage

After \(I\) and \(J\) merge, the distance from any other cluster \(K\) to \(I \cup J\) (Lance & Williams, 1967), with coefficients \(\alpha_I, \alpha_J, \beta, \gamma\) chosen by the linkage:

\[ d(K, I \cup J) = \alpha_I\, d(K, I) + \alpha_J\, d(K, J) + \beta\, d(I, J) + \gamma\, \lvert d(K, I) - d(K, J) \rvert \]
A B C 0 3 7 d(I, J) = 3 d(K, I) = 7 d(K, J) = 4 I = {A}, J = {B}, K = {C}
linkagecoefficients\(d(C, \{A, B\})\)
single\(\alpha = \tfrac12,\ \beta = 0,\ \gamma = -\tfrac12\)\(3.5 + 2 - 1.5 = 4\)
complete\(\alpha = \tfrac12,\ \beta = 0,\ \gamma = +\tfrac12\)\(3.5 + 2 + 1.5 = 7\)
average\(\alpha_I = \tfrac{|I|}{|I| + |J|},\ \beta = \gamma = 0\)\(3.5 + 2 = 5.5\)
The minimum, the maximum and the mean of the hand tables, from the distance matrix alone. So the algorithm never needs the points: any dissimilarity works, except for Ward, whose formula is exact only for squared Euclidean distances.

Beyond Euclidean distance

profiles: correlation distance

Four customers: two grow steadily, two spend in one peak month; one of each spends five times more.

Euclidean groups by how much they spend; \(1 - r\) groups by the shape.

documents: cosine distance

A text, the same text ten times longer, a text on another topic. From the first, Euclidean: 20.12 to the long copy, 3.87 to the other topic. Cosine: 0.0 and 1.0.

Which distance is right depends on the question; no index on the data can tell. Single, complete and average take any dissimilarity; Ward only Euclidean.
Stage 3 · how far does it scale?

Cost: why it does not scale

rowspairwise distances
1,0000.004 GB
10,0000.400 GB
100,00040 GB
1,000,000about 4,000 GB
Time of SciPy linkage grows like n to the 2.5 for Ward and 2.3 for single, against 0.004 s for k-means; memory 0.4 GB at 10,000 rows, 40 GB at 100,000

The best algorithms are \(O(n^2)\), still quadratic. On the machine that drew the figure, 16,000 rows took 6.11 s with Ward; k-means, 0.004 s. A method for hundreds to tens of thousands of rows.

What to do with more rows

  • Sample, then assign. Tree on a 2% sample (2,000 of 100,000 rows), cut into 5, every other row to the nearest centroid: ARI 0.993. What you lose is the fine structure: a group of 30 points would have about 0.6 representatives.
  • Connectivity constraints: merge only neighbors in a sparse graph.
  • HDBSCAN (hierarchical DBSCAN, density-based spatial clustering of applications with noise) is itself hierarchical: a single-linkage tree on a density-adjusted distance, without the full matrix, and it marks noise.
Stage 4 · when is a tree the right tool?

Hierarchical vs k-means vs DBSCAN

hierarchicalk-meansDBSCAN / HDBSCAN
a cluster isa branch of the merge treethe points nearest one centroida dense region
decided after fitting\(k\) or the cut height——
outputa nested family of partitionsone partitionone partition + noise
any distance?yes, except WardEuclidean onlyyes
new pointsno predictpredictno predict
same result every runyesdepends on the startyes

Applications

Taxonomies: whenever the product is a tree, not a partition.

Segmentation at several granularities: 12 fine segments that are subdivisions of 3 coarse ones.

Gene-expression heat maps: cluster rows and columns with correlation distance and redraw in leaf order.

A 40 genes by 12 samples table looks like noise in measured order and forms clear blocks in dendrogram leaf order; tumour and normal samples separate at the top

Same numbers, reordered by the tree: blocks of genes up and down in tumours appear, and the column tree splits tumour from normal samples at its top merge.

Summary

  1. Single follows chains and is fooled by bridges; complete, average and Ward find compact groups and cut thin shapes across.
  2. A local rule for the cut (the inconsistency coefficient) misses what the gaps, read from the top, see.
  3. One update formula: any dissimilarity works, except for Ward. Correlation distance for profiles, cosine for documents.
  4. Quadratic memory and time: hundreds to tens of thousands of rows. Beyond that: sample and assign, connectivity constraints, HDBSCAN.
  5. Choose a tree when the product is a hierarchy, the groups are unequal, or several levels of detail are needed.
Advanced lab · about 25 min, in pairs

The tree against k-means and the answer key, steps 7 to 9

  • Same notebook, same session, from step 7.
  • Step 7: k-means with \(k = 4\) and \(k = 7\); do its partitions nest like the tree's cuts?
  • Step 8: open the zoologists' classes and audit every decision of the fundamental lab.
  • Step 9: the heat map, rows and columns reordered by the tree.
  • Simulations: k-means with another seed (step 7) and the linkage, audited (step 8).

Pace: step 7 and its simulation in 10 min; step 8 and its simulation in 10 min; step 9 in 5 min.