Sub topic 9
Cost and limits of hierarchical clustering
This chapter assumes you can run linkage and AgglomerativeClustering as shown earlier, and that you know what a distance matrix is. Hierarchical clustering gives you a full tree, but that comes at a price in memory, time, and flexibility.
Why the cost grows so fast
Agglomerative clustering needs the distance between every pair of points. For n points that is n(n-1)/2 distances. scipy stores them as a condensed matrix of 8-byte floats, so memory grows with the square of n.
def condensed_size_mb(n):
"""Memory for the condensed distance matrix of n points, in megabytes."""
return n * (n - 1) / 2 * 8 / 1e6
for n in [1_000, 10_000, 100_000]:
print(n, round(condensed_size_mb(n), 1))| Points | Pairwise distances | Matrix memory |
|---|---|---|
| 1,000 | about 0.5 million | about 4 MB |
| 10,000 | about 50 million | about 400 MB |
| 100,000 | about 5 billion | about 40 GB |
Time is the second problem. A naive implementation scans for the closest pair at every merge, which costs O(n³). scipy's linkage uses faster algorithms for single, complete, average, and Ward linkage, and these run in O(n²) time. That is still quadratic, so a dataset ten times larger takes about a hundred times longer.
Limits of the method itself
- Merges are never undone. Each step is greedy. If an early merge is wrong, for example because of an outlier, every later merge builds on it.
- Linkage bakes in a shape bias. Single linkage can chain: a thin line of points links two separate groups into one cluster. Complete linkage and Ward favor compact, similar-sized groups and split elongated ones.
- Outliers distort results. Ward and complete linkage give outliers large influence. Single linkage tends to leave each outlier as its own tiny cluster, which hides the structure of the rest.
- Ward needs Euclidean distance. Its formula assumes squared Euclidean geometry. For other metrics, use average or complete linkage.
- No
predictfor new points.AgglomerativeClusteringhasfit_predictbut no way to assign a point it has not seen. You must refit or train a separate classifier on the labels. - You still choose the cut. The tree defers the choice of cluster count but does not remove it. A large gap in the dendrogram is a hint, not a proof.
- Scale still matters. Features with large ranges dominate the distances, as with any distance-based method.
Reducing the cost
scikit-learn's AgglomerativeClustering accepts a connectivity argument, a sparse graph (such as a k-nearest-neighbor graph) that restricts merges to neighboring points. This lowers the cost and also imposes local structure, which suits images or spatial data. Even so, the method rarely scales past tens of thousands of points in practice.
Exercise
You have 20,000 points and want to run scipy's linkage. Roughly how much memory does the condensed distance matrix need, and which two other problems would you check before running it?
Show answer
The matrix has 20,000 × 19,999 / 2, about 200 million, distances. At 8 bytes each that is about 1.6 GB. Before running, check that features are scaled, since large-range features dominate the distances. Also check that the linkage fits the expected cluster shapes. For example, single linkage may chain, and Ward requires Euclidean distance.