Machine Learning · Insper

Hierarchical Clustering

Fundamental approach

How merging the closest pair, again and again, builds a tree, and how the tree becomes groups

From one partition to a tree

k-means

needs \(k\) before it starts; returns one flat partition

hierarchical

builds all the groupings at once, nested, as a tree: the dendrogram. \(k\) is chosen afterwards, by cutting it.

The oldest way of organizing living things: kingdoms, classes, orders, families, species. Still what biologists, librarians and marketing teams want when one level of detail is not enough.

The path of this part

  1. Build the treemerge the closest pair; four linkages by hand
  2. Read the treeheights, not leaf order; the matrix Z
  3. Cut ita horizontal line; the gaps and the silhouette
  4. Check ithow faithfully the heights keep the distances
Each stage answers one question: how does merging, pair by pair, build a tree? → what does the tree say? → how does the tree become groups? → how faithful is the tree to the distances?
Stage 1 · how does merging, pair by pair, build a tree?

The idea: merge the two closest groups, repeat

  1. start with \(n\) clusters, one per point;
  2. find the two clusters closest to each other;
  3. merge them, and record the distance: the merge height;
  4. repeat until one cluster is left.

After \(n - 1\) merges, the history of the merges is the tree. Nothing random, nothing iterated to convergence: the same data always gives the same tree (up to ties).

Step 2 hides the only real decision: the distance between two clusters.

The linkage: distance between clusters

linkagedistance between \(P\) and \(Q\)tends to produce
singlethe closest pairchains; one bridge merges two groups
completethe farthest paircompact groups of similar diameter
averagethe mean of all cross distancesa compromise
Wardhow much the merge raises the within-cluster sum of squaresround groups of similar size, like k-means
\[ \Delta(P, Q) = \frac{|P|\,|Q|}{|P| + |Q|}\, \lVert \mu_P - \mu_Q \rVert^2 \]

The within-cluster sum of squares is the k-means inertia: Ward grows it as slowly as it can, one greedy merge at a time.

Simulator: merge, watch the tree grow, cut it

Predict. On the chain (two blobs and a bridge), which linkage will glue the two blobs together long before the last merge?

Five points, four linkages, step by step

A = 0, B = 3, C = 7, D = 12, E = 18 on a line: the gaps grow by one. Predict. After A + B, which merge comes next for each linkage? Then step through and switch the linkage.

One matrix, different trees

The five points and the four dendrograms; cut into two clusters, single and average give {ABCD}{E}, complete and Ward give {AB}{CDE}
Asked for two clusters: single and average isolate E; complete and Ward cut between B and C. Neither is wrong: they answer different definitions of "a cluster".
Stage 2 · what does the tree say?

Reading a dendrogram

  • The y-axis is a distance. Two points are as similar as the height of the lowest bar that joins them: the cophenetic distance. Complete tree: A and B at 3, A and E at 18.
  • Long vertical lines are the interesting part: a group that stays far from everything. {C, D, E} forms at 11 and waits until 18.
  • The left-to-right order carries almost no information: each bar can flip, so 5 leaves have \(2^4 = 16\) equivalent drawings.
In the complete tree, E is drawn right next to B, although they end up 18 apart. Read similarity from the height where they meet, never from neighboring leaves.

The linkage matrix Z


Z = linkage(X, 'complete')
# [[ 0.  1.  3.  2.]
#  [ 2.  3.  5.  2.]
#  [ 4.  6. 11.  3.]
#  [ 5.  7. 18.  5.]]
    
columnmeaning
0, 1the two clusters merged; ids 0 to \(n-1\) are points, row \(i\) creates cluster \(n + i\)
2the merge height: 3, 5, 11, 18
3how many points the new cluster has: 2, 2, 3, 5

One row per merge, \(n - 1\) rows, in the order the merges happened.

Stage 3 · how does the tree become groups?

Cutting the tree

A horizontal line at height \(t\): each piece hanging below it is one cluster.

Predict. Complete tree (heights 3, 5, 11, 18). Which clusters does a cut at 8 give?
cut atclusters
4{A, B}, {C}, {D}, {E}
8{A, B}, {C, D}, {E}
12{A, B}, {C, D, E}
20everything

Any cut between 11 and 18 gives the same two clusters: a range 7 units wide. That width is what makes the two-cluster answer stable here.

In scikit-learn, and one property


AgglomerativeClustering(n_clusters=2, linkage='ward').fit(X)
AgglomerativeClustering(n_clusters=None, distance_threshold=8, linkage='complete').fit(X)
  
  • The same merge heights as the hand tables (Ward on the \(\sqrt{2\Delta}\) scale).
  • No predict: a new point has no place in the tree until you rebuild it or assign it with another rule.
Cuts of the same tree are nested: the 3-cluster cut is the 2-cluster cut with one cluster split. Two k-means runs with \(k = 3\) and \(k = 4\) have no such obligation.

Choosing where to cut

400 points in four blobs that come in two pairs: two scales of structure. Ward tree.

\(k\)range of cut heightsgapsilhouette
232.3 to 159.2126.90.787
330.3 to 32.32.10.664
49.7 to 30.320.60.545
59.2 to 9.70.50.473
Ward tree of 400 points with cuts at 2 and 4 clusters; gap 126.9 for k=2 and 20.6 for k=4; silhouette falling from 0.787 to 0.545

The wider the range of heights a \(k\) survives, the more the data insist on it. The silhouette prefers the coarsest split and does not see the second scale.

So which cut is right?

  • Both: the cuts are nested. Report two segments at the coarse level and four at the fine level; they agree.
  • When one number is required, choose with the question in mind (how many groups can someone act on?) and use the gaps and the silhouette to rule out cuts the data do not support: \(k = 3\), gap 2.1.
Stage 4 · how faithful is the tree to the distances?

Cophenetic correlation, pair by pair

\(d\) = original distance of a pair; \(c\) = its cophenetic distance, the height where the two points first join. The cophenetic correlation is the correlation between \(d\) and \(c\) over the 10 pairs. Predict. Does the chain-shaped single tree keep the distances better or worse than the complete tree?

Faithful tree ≠ good clusters

The four blobs of the previous slides, four linkages. ARI (adjusted Rand index): 1 = the true groups, about 0 = chance.

cophenetic correlationARI at \(k = 4\)
single0.9440.496
complete0.9470.954
average0.9530.856
Ward0.9520.892
The cophenetic correlation measures how faithfully the tree represents the distances, not how good the clusters are. Use it when the tree itself is the product: a taxonomy, the order of a heat map.

Summary

  1. Merge the two closest clusters, repeat: the history of the merges is the tree.
  2. The linkage defines "closest" and decides what the tree finds: one matrix, three trees.
  3. Read heights, not the left-to-right order: two points are as similar as the bar where they meet.
  4. A cut is a partition, and the cuts of one tree are nested; the widest range of heights points to \(k\).
  5. Cophenetic correlation = how faithful the tree is to the distances, not how good the clusters are.
symptomwhat to do
every \(k\) survives only a narrow range of heightsreport the tree; do not force one \(k\)
two cuts, both with wide gapskeep both levels: they are nested
Fundamental lab · about 30 min, in pairs

A tree of 101 animals, steps 1 to 6

  • The UCI (University of California, Irvine) Zoo table: 15 yes/no questions and the number of legs. The zoologists' class stays sealed until step 8, in the advanced part.
  • Scale the columns, build four trees and keep the most faithful, draw it, choose the cut and name the clusters.
  • Download the lab notebook and open it in Jupyter or Colab.
  • At every step, answer the Predict before running.
  • Simulations: the tree without scaling (step 2) and a new animal, without predict (step 6).

Pace: steps 1 and 2 and the first simulation in 10 min; steps 3 to 5 in 10 min; step 6 and the second simulation in 10 min.