Module 10 of 10 · Beginner track

Hierarchical clustering — the family tree of your data

The final module drops the labels. Given only points, find the groups — but this time without k-means' awkward demand that you announce the number of clusters up front. Agglomerative clustering sidesteps it with a simple bottom-up ritual: start with every point as its own cluster, then repeatedly merge the two closest clusters, recording the distance at which each merge happened, until one cluster remains.

The record of merges is the output: the dendrogram, a tree whose joints sit at their merge distances. Low joints are easy, obvious unions; a tall joint is the algorithm holding its nose while gluing two genuinely different groups. To get concrete clusters, slice the tree at any height — and here's the elegance: one tree contains the clustering for every possible K at once. Slide the cut; the answer reorganizes instantly, no refitting.

One definitional choice hides real consequences: what does “distance between clusters” mean once clusters hold many points? Single linkage (closest pair) chains through touching neighbors and grows straggly clusters; complete linkage (farthest pair) demands total cohesion and grows compact ones; averagesplits the difference. Same data, different trees — try all three below.

🎛 Dendrogram lab

clusters at this cut: 3
2.5

Dendrogram — merge history, bottom-up

The same points in feature space, colored by the cut

Reading a dendrogram: each point starts as its own cluster at the bottom; every ∏-shaped joint is a merge, drawn at the height (distance) at which it happened. Low joints = tight, obvious groupings; a tall joint means two genuinely different groups got forced together. Slide the red cut line: cross below a joint and its cluster splits — one tree contains the answer for every K at once, no re-running (k-means can't do that). Linkage is the personality: single chains through nearest points (elongated, straggly clusters), complete demands total cohesion (compact blobs), average sits between. Educational tool.

Practical notes, and the other half of the chapter

Distances are only meaningful on comparable scales — standardize features first, or the widest-ranged one silently defines “close.” And clustering's permanent caveat applies: the ritual always completes, structure or no structure, so validate before believing (bootstrap stability, silhouette scores). The chapter's other pillar — flat clustering with k-means and dimension reduction with PCA — is built hands-on in the advanced track's unsupervised module. In markets, dendrograms have a direct application: cluster assets by return correlation and the tree becomes a diversification map — assets merging low are near-substitutes, and a portfolio's true breadth is roughly the count of tall branches.

Things to try

  • • Set the cut just below the tallest joint: 2 clusters. Just below the next: 3 — matching the true blobs. Every K lives at some height.
  • • Switch complete → single linkage and watch the tree's shape change: single's joints climb gradually (chaining), complete's show a cleaner tall-vs-short separation on blobby data like this.
  • • Find a point that switches color across linkages — border points are genuinely ambiguous, and honest clustering admits it.