Agglomerative Clustering
Agglomerative clustering starts with each point as its own cluster. Every merge joins two clusters into one, so the cluster count drops by exactly one per merge.
Predicting How Clustering Can Begin
Imagine you are handed a pile of data points and asked to sort them into groups, but nobody has told you how many groups to expect or what each group should look like. A natural first move is to ask which points belong together, and then to keep combining the most similar groups until the result is manageable. Agglomerative clustering is a family of algorithms built on exactly this idea.
What do you think happens?
If there are no groups yet, what would be a reasonable first arrangement of the data before any merging happens?
Reveal answer
Every data point is placed in its own cluster, so the starting state has as many clusters as there are points.
Agglomerative clustering starts from the simplest possible state, with each point as its own cluster. The rest of the procedure is built on top of that starting arrangement.
The word agglomerative refers to the act of gathering things together, and that is the direction the whole procedure moves in. Instead of splitting one large dataset into smaller pieces, the algorithm builds larger and larger groups out of smaller ones.
Starting From Single Points
Agglomerative clustering refers to a collection of clustering algorithms that all build upon the same principles. Each algorithm in the family begins in the same way: every point is declared to be its own cluster. So if there are twenty data points, the algorithm starts with twenty clusters, each containing exactly one point. Nothing has been grouped yet.
This starting point is what gives the family its defining behavior. Because every point begins alone, the first decisions are always about pairs of single points, and the groups grow only through the merges that the algorithm performs afterward.
Agglomerative clustering starts with each point as its own cluster. Every merge joins two clusters into one, so the cluster count drops by exactly one per merge.
The Repeated Merge Loop
From that starting arrangement, the algorithm repeats one operation. It finds the two most similar clusters that currently exist and merges them into a single cluster. The procedure does not jump to a final answer. It moves through a sequence of intermediate arrangements, each one slightly coarser than the one before.
Notice that the cluster count is the only thing the loop diagram tracks. What decides which two clusters are merged at each step is a separate question, and it is where the linkage criteria come in. Read the loop as a cycle with an exit: the algorithm keeps returning to the search for the most similar pair until the cluster count matches the stopping condition, and only then does it stop.
Tracing a Three-Cluster Run
Consider a two-dimensional dataset where you want three clusters. The source describes the progression in general terms: two single-point clusters are picked and joined into a two-point cluster, and later one of those two-point clusters is extended to include a third point. The concrete numbers below are an illustration built on that pattern.
Six points reduced to three clusters
Six data points are each placed in their own cluster. The target is three clusters. Trace the cluster count through the merges.
- 1Start
Six points, so six clusters, each holding one point. The cluster count is 6.
- 2Merge 1
The two most similar single-point clusters are joined into one two-point cluster. The count drops to 5.
- 3Merge 2
The algorithm compares the clusters that now exist, which include the new two-point cluster. It joins two of them, and the count drops to 4.
- 4Merge 3
The two-point cluster is extended to include a third point, so the count drops to 3. This matches the target, so the loop stops.
Three clusters remain. The algorithm never examined the whole dataset at once. At each step it only decided which two current clusters to combine next.
Why Cluster Distance Needs a Linkage Criterion
The loop has a hidden difficulty. When every cluster holds just one point, asking which two clusters are closest is easy, because it is simply the distance between two points. Once a cluster holds several points, the question becomes ambiguous. A cluster is not a single location, so what does it mean for two clusters to be close?
The answer to that question is the linkage criterion. It specifies exactly how the similarity between two clusters is measured, and the choice of criterion changes which clusters get merged and therefore the final result. A linkage criterion is needed in every merge step, because every merge step compares a pair of clusters that may each contain several points.
Three Linkage Criteria
Three linkage criteria are implemented in scikit-learn. Each one answers the question of how to measure the closeness of two clusters, and each leads to a different kind of merging behavior. Each criterion summarizes the relationship between two clusters in a different way.
Average Versus Complete on a Number Line
Now test the two distance-based criteria on a case where they should disagree. Suppose four clusters already exist on a number line. Cluster P holds the single point 0. Cluster Q holds the points 1 and 5. Cluster R holds the single point 10. Cluster S holds the points 13 and 14.
What do you think happens?
Which pair does average linkage merge first, and which pair does complete linkage merge first?
Reveal answer
Average linkage merges P and Q; complete linkage merges R and S.
For average linkage, the mean cross-cluster distance between P and Q is $\frac{1+5}{2} = 3$, which is the smallest mean among the candidate pairs. For complete linkage, the maximum cross-cluster distance between R and S is 4, which is the smallest maximum among the candidate pairs. The two criteria therefore pick different pairs.
Checking the candidate pairs
For each candidate pair, compute the cross-cluster distances between their points, then take the mean (average linkage) and the maximum (complete linkage).
- 1Pair P and Q
Distances from 0 to 1 and to 5 are 1 and 5. The mean is $\frac{1+5}{2} = 3$ and the maximum is 5.
- 2Pair R and S
Distances from 10 to 13 and to 14 are 3 and 4. The mean is $\frac{3+4}{2} = 3.5$ and the maximum is 4.
- 3Pair Q and R
Distances from 1 and 5 to 10 are 9 and 5. The mean is 7 and the maximum is 9.
- 4Pair Q and S
Distances from 1 and 5 to 13 and 14 are 12, 13, 8, and 9. The mean is 10.5 and the maximum is 13.
- 5Pair P and R, P and S
P to R has a single distance of 10, with mean and maximum both 10. P to S has distances 13 and 14, with mean 13.5 and maximum 14.
- 6Choose the smallest
Average linkage picks the smallest mean, which is P and Q at 3. Complete linkage picks the smallest maximum, which is R and S at 4.
Average linkage merges P and Q. Complete linkage merges R and S. The two criteria disagree because they summarize the same cross-cluster distances differently.
Stopping at a Target Count
Merging cannot go on forever, so the algorithm needs a rule for when to stop. In general terms, the algorithm merges the two most similar clusters until some stopping criterion is satisfied. The source specifies which stopping criterion is implemented in scikit-learn: the number of clusters. The user specifies a target number of clusters, and similar clusters are merged until only that number of clusters remain.
- 1Set the target number of clusters before running the algorithm.
- 2Start with one cluster per point.
- 3Find the two most similar current clusters using the chosen linkage criterion and merge them.
- 4Check the current cluster count against the target.
- 5If the count is still above the target, return to the search step. If it matches the target, stop.
Two things are worth noticing. The algorithm never looked at the target until it was time to decide whether to stop, so the target only controlled when the loop ended. The stopping condition is checked against the current state of the clusters, not against the original points.
Mistakes When Reading Linkage Choices
Common mistake Assuming the algorithm compares every point in the dataset at once.
Why it failsThe algorithm only decides which two current clusters to combine next at each step.
Do this insteadThink of each step as a local decision between two existing clusters.
Common mistake Treating the cluster count as the same thing as the linkage criterion.
What it looks likeThe count tells you when to stop, while the linkage criterion tells you which pair to merge at each step.
Why it failsThe diagram of the loop tracks only the cluster count. The choice of which pair merges is a separate question.
Do this insteadKeep the stopping rule and the merge rule separate in your mind.
Common mistake Expecting average and complete linkage to always merge the same pair.
What it looks likeOn the number line with P at 0, Q at 1 and 5, R at 10, and S at 13 and 14, average merges P and Q while complete merges R and S.
Why it failsDifferent criteria can merge different pairs because they summarize the same cross-cluster distances in different ways.
Do this insteadCompute the criterion you are using explicitly before predicting the merge.
Common mistake Assuming a cluster is a single location when measuring closeness.
Why it failsA cluster holds several points, so closeness between clusters is ambiguous until a linkage criterion is chosen.
Do this insteadName the criterion that defines the distance between the two clusters.
Choosing a Linkage Criterion
A sensible workflow is to begin with Ward, since the source describes it as the default and as working on most datasets. Then look at the expected sizes of your clusters. If the clusters are likely to have very dissimilar numbers of members, for example one group much larger than all the others, average or complete linkage may be worth trying in its place.
Start with Ward as the default. Switch to average or complete linkage when you expect clusters of very different sizes, and compare the resulting groupings rather than assuming one criterion is always correct.
Suppose you expect one cluster to contain far more points than the others. Which linkage criterion would you try first, and what would you check in the output to decide whether it was a good choice?
Hints
- Recall that different criteria can merge different pairs when cluster sizes differ.
- Compare the groupings produced by two criteria on the same data.
Key Takeaways
- Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two most similar clusters, reducing the cluster count by one per merge.
- The linkage criterion is needed because a cluster holds several points, so the closeness of two clusters is not defined until a rule is chosen.
- Ward merges the pair that least increases within-cluster variance, average uses the mean cross-cluster distance, and complete uses the maximum; they can merge different pairs on the same data.
- In scikit-learn, merging stops when the number of clusters reaches the target you specified in advance.
- Ward is the default starting point, while average or complete linkage may suit datasets whose clusters differ greatly in size.