Part VI — Unsupervised Learning, Clustering, Dimensionality & Time Series · Chapter 3

Hierarchical & agglomerative clustering

Hook

k-means makes you commit to a number of clusters before you've even looked at the data. What if, instead, you built every possible clustering at once — from 5 separate points all the way down to 1 big cluster — and picked afterward?

Intuition
Start: every point is its own cluster

Step through the merges one at a time. The closest pair joins first, then the next-closest pair or point, and so on, until everything is one cluster. Nothing here ever needed to be told how many groups to expect.

Formalize

Agglomerative clustering starts with every point in its own cluster and repeatedly merges the two closest clusters, using single linkage — the distance between a pair of clusters is the distance between their single closest members:

dsingle(A,B)=miniA, jBxixjd_{\text{single}}(A, B) = \min_{i \in A,\ j \in B} \lVert x_i - x_j \rVert
  • A,BA, B — two clusters, each a set of points.
  • xi,xjx_i, x_j — a point from AA and a point from BB.
  • dsingle(A,B)d_{\text{single}}(A,B) — the smallest distance between any pair drawn one from each cluster.
  1. Start with n singleton clusters

    Every point begins as its own one-member cluster — no assumption about count or shape yet.

  2. Merge the closest pair of clusters

    Compute dsingled_{\text{single}} between every pair of current clusters, and merge whichever pair is smallest.

  3. Record the merge height

    That minimum distance becomes the merge's height — how far apart the two groups were when they joined. Stacking every merge by height, in order, draws the dendrogram.

  4. Repeat until one cluster remains

    Keep merging the closest pair, one merge at a time, until every point belongs to a single cluster. Cutting the finished dendrogram at any height recovers a full clustering — no re-running required.

Play
5 cluster(s) at height 0.0

Cut near height 0 and every point is alone. Cut anywhere between the tight inner merges and the one big final jump, and the two true groups fall right out. Push the cut past that final jump, and everything collapses into a single cluster — the same tradeoff kk posed for k-means, now expressed as where to slice, not how many bins to pre-declare.

Worked example

Five points on a line — 0,1,20, 1, 2 close together, then 6,76, 7 close together, with a wide gap between the two groups:

  1. Closest pair merges first, at height 1

    Points at 00 and 11 are exactly 11 apart — the smallest distance anywhere in the data. They merge first, at height 11.

  2. The third point joins at the same height

    Point 22 is distance 11 from point 11 (single linkage only needs the closest member) — it joins {0,1}\{0,1\}, also at height 11.

  3. The other pair merges independently, also at height 1

    Points 66 and 77 merge into their own cluster, at height 11 — completely unrelated to the first group so far.

  4. The two groups finally join at height 4

    The closest pair across the groups is 22 and 66, distance 44 apart — a jump four times larger than any merge before it. That's the dendrogram's one big gap, and exactly where cutting recovers the two true clusters.

Checkpoint

Drag the cut height until the dendrogram splits into exactly 2 clusters — the two true groups, A, B, C and D, E.

5 cluster(s) at height 0.0
Drag the cut height to try it
Summary
dsingle(A,B)=miniA, jBxixjd_{\text{single}}(A, B) = \min_{i \in A,\ j \in B} \lVert x_i - x_j \rVert

Agglomerative clustering trades k-means' single answer for the entire family of clusterings, stacked by merge height in a dendrogram — you decide how many clusters you want after seeing the structure, by choosing where to cut. The cost is speed: computing every pairwise cluster distance at every merge is far more expensive than k-means' handful of centroid updates, which is exactly why k-means remains the default choice for very large datasets.