Search

Search papers and lessons

Hierarchical Clustering

Level 2 · 11 min read

Hierarchical clustering starts with every point alone and repeatedly merges the two closest groups, recording each merge so that the whole history can be cut at any level.

On this page

  • Colour = topic mix
  • Linear algebra
  • Learning basics
  • Discrete math
  • ReadReady to readNot yet

TL;DR

Hierarchical clustering groups unlabelled points by starting with every point in its own group and repeatedly merging the two closest groups. Each merge is recorded, so the result is a tree, called a dendrogram, that shows the groups at every scale. You choose the number of groups afterwards by cutting the tree at a height, which is its main advantage over methods that need the count fixed in advance such as k-means.

Builds on: Euclidean Distance · Leads to: K-Means Clustering, Binary Tree

Where it's used

Customer segments in retail. A chain has sales figures for each store across two products and wants to see which stores behave alike. It can merge stores step by step and then decide, from the tree, how many segments to report. Analysts often look at the dendrogram before fixing the number of segments.

Gene expression in biology. Researchers measure how strongly thousands of genes switch on across tissue samples. Clustering the genes hierarchically groups genes that rise and fall together, and the tree is drawn beside the data so that a reader can see which genes belong to the same family.

Document collections. A library's articles can be grouped by how many words they share. A merge tree lets an editor move from broad topics at the top to narrow subtopics at the bottom without rerunning the method.

How it works

Take five points on a line: A = 1, B = 2, C = 5, D = 6 and E = 10. The distance between two points is the absolute difference of their values. Here is the full set of distances:

  • A-B 1, A-C 4, A-D 5, A-E 9
  • B-C 3, B-D 4, B-E 8
  • C-D 1, C-E 5
  • D-E 4
flowchart TD
  S[Start: every point is its own cluster] --> F[Find the two closest clusters]
  F --> M[Merge them and record the distance]
  M --> Q{More than one cluster left?}
  Q -- yes --> F
  Q -- no --> T[Cut the tree at a height to choose groups]

We use the single linkage rule for distance between groups: the distance between two groups is the smallest distance between any point in one and any point in the other.

  1. Start with five clusters: {A}, {B}, {C}, {D}, {E}. Check: five groups, each one point.

  2. Find the closest pair. A-B and C-D are both at distance 1. Merge A and B, and record height 1. Check: now there are four clusters, {A, B}, {C}, {D}, {E}.

  3. Find the closest pair of clusters again. The distance from {A, B} to C is the smaller of A-C = 4 and B-C = 3, which is 3. The distance from C to D is 1, which is smallest. Merge C and D, and record height 1. Check: three clusters, {A, B}, {C, D}, {E}.

  4. Find the closest pair. Between {A, B} and {C, D} the smallest distance is B-C = 3. Between {C, D} and E it is D-E = 4. Between {A, B} and E it is B-E = 8. The smallest is 3, so merge {A, B} with {C, D}, and record height 3. Check: two clusters, {A, B, C, D} and {E}.

  5. Merge the last two. The distance from {A, B, C, D} to E is D-E = 4. Record height 4. Check: one cluster containing everything.

The recorded heights are 1, 1, 3, 4. The tree is complete. To get groups, cut it at a height. Cutting at height 2 keeps every merge below 2 and gives {A, B}, {C, D}, {E}. Cutting at height 3.5 keeps merges below 3.5 and gives {A, B, C, D} and {E}.

The math

Definition: Linkage

Let d(x,y)d(x, y) be the distance between two points, here the Euclidean distance. For two clusters PP and QQ, a linkage D(P,Q)D(P, Q) turns point distances into a cluster distance. The three common choices are the minimum, Dsingle(P,Q)=min⁡x∈P, y∈Qd(x,y)D_{\text{single}}(P, Q) = \min_{x \in P,\, y \in Q} d(x, y), the maximum, Dcomplete(P,Q)=max⁡x∈P, y∈Qd(x,y)D_{\text{complete}}(P, Q) = \max_{x \in P,\, y \in Q} d(x, y), and the average, Daverage(P,Q)=1∣P∣ ∣Q∣∑x∈P∑y∈Qd(x,y)D_{\text{average}}(P, Q) = \frac{1}{|P|\,|Q|}\sum_{x \in P}\sum_{y \in Q} d(x, y).

Step 2 in symbols: merging the closest pair

At each step the algorithm picks the pair of clusters (P,Q)(P, Q) with the smallest D(P,Q)D(P, Q), merges them into P∪QP \cup Q, and records the value of D(P,Q)D(P, Q) as the merge height. With nn points there are exactly n−1n - 1 merges, so the process always ends in one cluster.

Step 3 in symbols: the single-linkage update

Step 3 of the walkthrough needed the distance from the new cluster {A,B}\{A, B\} to the point C. With single linkage this is a minimum over the two members: Dsingle({A,B},{C})=min⁡(d(A,C), d(B,C))=min⁡(4,3)=3.D_{\text{single}}(\{A, B\}, \{C\}) = \min(d(A, C),\, d(B, C)) = \min(4, 3) = 3. There is no need to recompute from raw points; one can keep a Matrix of cluster distances and update only the row of the new cluster.

Step 4 in symbols: cutting the tree

If the merge heights are h1≤h2≤⋯≤hn−1h_1 \le h_2 \le \cdots \le h_{n-1}, cutting at height tt keeps every merge with height at most tt. The number of groups is nn minus the number of merges kept. In the example, merges at heights 1, 1 and 3 are kept at t=3.5t = 3.5, which leaves 5−3=25 - 3 = 2 groups.

Worked examples

Example
1. Tiny case: three points on a line

Points 0, 1 and 3, with single linkage.

  1. Distances: d(0,1)=1d(0, 1) = 1, d(0,3)=3d(0, 3) = 3, d(1,3)=2d(1, 3) = 2.
  2. Merge the closest pair, 0 and 1, at height 1.
  3. The distance from {0, 1} to 3 is min⁡(3,2)=2\min(3, 2) = 2.
  4. Merge at height 2. The heights are 1 and 2.
  5. Interpretation: cutting at height 1.5 keeps only the first merge, so the groups are {0, 1} and {3}. Cutting at 2.5 gives one group.
Example
2. Standard case: the five points

Use the walkthrough. Compare single and complete linkage in the third merge.

  1. Single linkage: the distance from {A, B} to {C, D} is the minimum of A-C 4, A-D 5, B-C 3 and B-D 4, which is 3.
  2. Complete linkage: the same distance is the maximum of those four numbers, which is 5.
  3. So the two rules disagree about how far apart the groups are, and the tree changes. Under complete linkage the first two merges are the same: A-B at height 1 and C-D at height 1. Then {A, B} to {C, D} is at distance 5, and {C, D} to {E} is max⁡(5,4)=5\max(5, 4) = 5. This is a tie. Either merge gives the same heights from here on.
  4. Merge {A, B} with {C, D} at height 5. The distance from {A, B, C, D} to {E} is max⁡(9,8,5,4)=9\max(9, 8, 5, 4) = 9, so the last merge is at height 9. The complete-linkage heights are 1, 1, 5, 9, against 1, 1, 3, 4 for single linkage.
  5. Interpretation: the linkage rule decides the shape of the tree. Single linkage follows chains of close points, and complete linkage prefers compact groups.
Example
3. Realistic case: four stores by two product sales

Four stores report weekly sales of two products, in hundreds of units. The points are S1 = (1, 1), S2 = (1, 2), S3 = (5, 5) and S4 = (6, 5). Use Euclidean distance and single linkage.

  1. Distance S1-S2 is 02+12=1\sqrt{0^2 + 1^2} = 1.
  2. Distance S3-S4 is 12+02=1\sqrt{1^2 + 0^2} = 1.
  3. Merge S1 and S2 at height 1, and merge S3 and S4 at height 1.
  4. Distance S2-S3 is 42+32=25=5\sqrt{4^2 + 3^2} = \sqrt{25} = 5. This is the smallest distance between the two groups, since S1-S3 is about 5.66, S2-S4 is about 5.83 and S1-S4 is about 6.40.
  5. Merge {S1, S2} with {S3, S4} at height 5.
  6. Interpretation: cutting at height 2 gives two segments, low sellers {S1, S2} and high sellers {S3, S4}. Cutting at 6 gives one group, so the tree shows the two-segment split clearly.

Try it: Using the same four stores, at what height do the two groups merge if the linkage is complete instead of single? Answer: the complete-linkage distance is the maximum of the four cross distances, which is S1-S4 =25+16=41≈6.40= \sqrt{25 + 16} = \sqrt{41} \approx 6.40. So the merge is at about 6.40, not 5.

Common mistakes and tradeoffs

It is tempting to think that the dendrogram's heights are the numbers of clusters. In fact a height is a distance at which two groups were joined, and the number of groups is found by counting how many merges lie below the cut.

It is tempting to think that the linkage rule is a detail. In fact single linkage tends to chain points into long stretched groups, while complete linkage produces compact groups; the same data can give different segments under the two rules.

It is tempting to think that a merge can be undone later. In fact the algorithm is greedy: each merge is fixed once made. A poor early merge stays in the tree, so the result depends on the order of merges and on how ties are broken.

It is tempting to think that the distance function is always Euclidean. In fact the features should be on comparable scales first, or one large-valued feature will dominate. For text, Cosine Similarity is often a better measure than Euclidean distance.

When to use it: when you want to see the structure at several scales, when the number of groups is not known in advance, or when the data set is small enough that the pairwise distances fit in memory.

When not to use it: when the data set is very large, because the naive version takes roughly cubic time in the number of points. Use k-means or a sampling method then.

Alternatives: k-means when you know the number of groups and the groups are roughly round; density-based methods when groups have irregular shapes.

History

In the 1950s and 1960s, biologists who wanted to group species by shared traits needed a way to build family trees from measured distances. Robert Sokal and Peter Sneath set out much of this numerical approach in their 1963 book Principles of Numerical Taxonomy.

In 1967, Stephen Johnson wrote a paper that set out hierarchical clustering schemes in general terms, including the single, complete and average linkage rules used today. It made the method a standard tool in statistics and data analysis.

It caught on because the tree answers several questions at once. A researcher can see the broad structure at the top and the fine structure at the bottom, and can choose a cut after seeing the data, rather than deciding the number of groups beforehand.

Recap

  • Hierarchical clustering starts with every point alone and merges the two closest groups at each step.
  • The linkage rule, single, complete or average, defines the distance between two groups.
  • Each merge is recorded at a height, and the full record is the dendrogram.
  • Cutting the dendrogram at a height gives the groups; the number of groups depends on the cut.
  • The method is greedy and can be slow on large data, which is why K-Means Clustering is often used on large data sets.

Read next: K-Means Clustering for the flat alternative, and Binary Tree for the tree structure behind the dendrogram.

Check yourself

QuestionPoints 0, 2 and 3 are clustered with single linkage. What is the first merge and at what height?Show answer

The closest pair is 2 and 3, at distance 1. So the first merge is {2, 3} at height 1. The pair 0 and 2 is at distance 2 and 0 and 3 at distance 3, so they are not closer.

QuestionUsing the five points A = 1, B = 2, C = 5, D = 6, E = 10, what is the single-linkage distance between {C, D} and {E}?Show answer

It is the smallest of the distances from C to E and D to E. These are ∣5−10∣=5|5 - 10| = 5 and ∣6−10∣=4|6 - 10| = 4. The minimum is 4, so the distance is 4.

QuestionWhy can a dendrogram's merge heights not be read as the number of clusters?Show answer

Each height is a distance at which two groups were joined, not a count. The number of clusters at a given cut equals the number of points minus the number of merges kept below that height, so you have to count the merges below the cut to find it.

QuestionA chain of stores is arranged along a road with each store 1 km from the next. Explain why single linkage may merge the whole chain into one group at a small height, while complete linkage may not.Show answer

Single linkage uses the minimum distance between groups, so each new store is joined to the group at distance 1 from its neighbour, and the chain grows one link at a time at height 1. Complete linkage uses the maximum distance, so the group's far end is far from the next store, and the merge is recorded at a larger height that grows as the chain lengthens.

QuestionYou have 6 points and cut the dendrogram so that 4 merges lie below the cut. How many groups are there, and why?Show answer

There are 6−4=26 - 4 = 2 groups. Each merge joins two groups into one, so each kept merge reduces the number of groups by one, starting from 6 single points.

Sources