Sub topic 5
Linkage criteria and the clusters they favor
This chapter assumes you know the agglomerative procedure from the previous chapter: start with every point as its own cluster and repeatedly merge the closest pair. You should also be comfortable with Euclidean distance and scikit-learn's fit_predict.
The missing definition
Distance between two points is easy to compute. Agglomerative clustering also needs the distance between two clusters, which each hold many points. A linkage criterion is the rule that turns point distances into a cluster distance. Different rules rank the same pairs differently, so they produce different trees.
The four common criteria
| Linkage | Cluster distance | Favors | Typical failure |
|---|---|---|---|
| Single | Smallest distance between any two points, one from each cluster | Elongated or non-convex shapes | Chaining: a line of stray points joins two clusters |
| Complete | Largest distance between any two points, one from each cluster | Compact clusters of similar diameter | Splits large clusters, sensitive to outliers |
| Average | Mean of all pairwise distances | A compromise between the two above | Biased toward roughly spherical clusters |
| Ward | Increase in total within-cluster variance if the two merged | Compact, similar-sized clusters | Requires Euclidean distance, struggles with irregular shapes |
Single linkage only needs one close pair, so a thin bridge of points is enough to merge two clusters. Complete linkage looks at the farthest pair, so it refuses to merge clusters that have any far-apart members. Ward merges whichever pair increases the squared spread the least. This makes it behave like k-means, which is why it often gives balanced clusters on blob-like data.
Comparing linkages on the same data
Two interleaved half-moons are a good test. They are not convex, so criteria that favor compact clusters should struggle.
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_moons
X, _ = make_moons(n_samples=300, noise=0.05, random_state=0)
for link in ["single", "complete", "average", "ward"]:
model = AgglomerativeClustering(n_clusters=2, linkage=link)
labels = model.fit_predict(X)
print(link, np.bincount(labels))Single linkage typically recovers the two moons with sizes near 150 each, because each moon is a connected chain of nearby points. The other three tend to cut across the moons, since they prefer compact groups. Compare the printed sizes, then try noise=0.2 and watch single linkage degrade as the gap between the moons fills with points.
Choosing a starting point
- Start with Ward when you expect compact, similar-sized groups and your data is numeric.
- Use average linkage when you need a non-Euclidean metric, such as cosine distance, or want something less extreme than single and complete.
- Use single linkage only when you expect elongated shapes and have little noise.
- Use complete linkage when you want tight clusters and have checked for outliers.
The linkage choice changes the dendrogram's shape and its merge heights, so it also affects where you would cut the tree. The next chapter covers reading those heights.
Exercise
You run single linkage on customer data and get one huge cluster plus many singletons. Which property of single linkage likely caused this, and which linkage would you try next?
Show answer
Chaining. Single linkage merges two clusters when any one pair of points is close, so a trail of intermediate points absorbs everything into one cluster. Ward (or complete) linkage is a reasonable next try, because both favor compact clusters. Scale the features first.