← All AI/ML articles

Hierarchical clustering, visualised.

No labels, no k to guess up front. Start with every point alone, keep gluing the two closest groups together, and read the whole story off a tree.

1. Merge the closest2. Read the tree3. Cut where you like
Click empty space to add a point · drag to move · double-click to remove. Dashed line = the next merge.
Clusters0
Points0
Cut height0
Behind the scenes · 1

Who is closest to whom?

Everything starts from distances. Here are the straight-line (Euclidean) distances between the first six points. The smallest distance in this six-point subset is highlighted. In the full dataset, the algorithm merges whichever pair of clusters has the smallest distance under the selected linkage, so the first merge may involve points not shown here.


d(p, q) = √((xp − xq)² + (yp − yq)²). Hierarchical clustering only needs this table, never the labels.

Behind the scenes · 2

The dendrogram: the full merge story

Each U-shape is one merge. Its height is the distance at which the two groups joined. Drag the white line up or down: everything below it is glued together, and the branches it crosses are your clusters.


A large gap between successive merge heights can hint at a useful cut.

Highlighted rows are the merges already done at your current k. A merge that happens at a much greater height than the one before it joins groups that are really far apart.

Behind the scenes · 3

The algorithm in six lines

1. Put every point in its own cluster. 2. Compute the distance between every pair of clusters. 3. Merge the two closest clusters. 4. Record the merge and its distance (the dendrogram height). 5. Repeat 2-4 until one cluster is left. 6. Cut the tree at a height (or at k clusters).

The only choice that really matters is step 2: how to measure the distance between two groups. That is the linkage.

The mathematics

The mathematics behind hierarchical clustering

Numbers below update live with your data, linkage and k.

A. Point-to-point distance

d(p, q) = √ Σj (pj − qj)²

B. Group-to-group distance (linkage)

single = min d(a, b)

Closest pair across groups. Can chain long snakes together.

complete = max d(a, b)

Farthest pair. Prefers compact, similar-size groups.

average = mean d(a, b)

Mean over all cross pairs. A balanced middle ground.

Ward = √(2·nAnB/(nA+nB)) · ‖cA − cB‖

Merge that increases within-cluster variance the least (c = centroid).

Ward convention. The increase in within-cluster sum of squares is ΔSSE = (nAnB/(nA+nB)) · ‖cA − cB‖². This page reports the distance form dW = √(2·ΔSSE), the same scaling used by SciPy, so it is directly comparable to the height axis. Some textbooks and tools use ΔSSE itself as the merge cost; the merge order is identical, only the height scale differs. Because Ward is built on variance, it assumes Euclidean distances and should not be combined with arbitrary metrics. Its value also differs in definition from single, complete and average linkage, so compare it to them with care.

C. Worked example: the next merge

The same two clusters, measured four different ways. Whichever linkage you pick decides who merges first.


D. Reading the tree

Rules of thumb
  • Height = distance at which two groups joined.
  • Only the vertical positions mean anything. Left/right order of leaves is arbitrary.
  • A large gap between successive merge heights can suggest a useful cut, because the next merge joins groups at a much greater distance. Treat this as a guide, not a guarantee that the resulting clusters are the only correct grouping.
  • Complexity is about O(n³) naively (O(n²) memory), so it suits thousands of points, not millions.
Experiment

Break it on purpose

Chaining

Two blobs joined by a thin bridge, with single linkage. Watch them fuse into one long chain.

One far outlier

Add a distant outlier under complete linkage. Watch how long it remains separate before joining the hierarchy.

Same data, new linkage

Cycle through all four linkages on the same points and see how the tree changes shape.

Check yourself

Three quick questions

Key takeaways

  • Agglomerative clustering starts with single points and repeatedly merges the closest two groups.
  • The dendrogram records every merge, so you choose k after the fact by cutting the tree.
  • Linkage defines “closest” for groups: single chains, complete is compact, average balances, Ward minimises variance.
  • Check scale and outliers: standardise features when their units or ranges would otherwise distort the distances, and choose the scaling method to suit the data. Sometimes the original units are meaningful.
  • Cost grows fast with n, so it is best for small to medium datasets.
← Back to all articles