Hierarchical Clustering
Hierarchical clustering builds a tree of nested groups, letting you read clusters at any granularity from a dendrogram.
A tree of clusters
Hierarchical clustering produces a nested hierarchy rather than a single partition. Agglomerative (bottom-up) methods start with each point as its own cluster and repeatedly merge the two closest clusters; divisive (top-down) methods start with one cluster and split. The result is a dendrogram, a tree you can cut at any height to get a chosen number of clusters.
Linkage: how to measure cluster distance
- Single linkage: distance between the two nearest points; finds chains, sensitive to noise.
- Complete linkage: distance between the two farthest points; compact clusters.
- Average linkage: mean pairwise distance; a middle ground.
- Ward linkage: merges that least increase within-cluster variance; often the best default.
from scipy.cluster.hierarchy import linkage, fcluster
Z = linkage(X, method='ward')
labels = fcluster(Z, t=4, criterion='maxclust')
Reading a dendrogram
The height at which two clusters merge reflects how dissimilar they are. Long vertical gaps suggest natural cluster counts; cutting across a tall gap gives a stable grouping. This visual, no-k-required view is a key advantage over k-means.
Costs and uses
Agglomerative clustering costs roughly O(n^2) memory and time, so it suits small to medium datasets. It shines when the data has genuine nested structure, such as taxonomies or grouped sensor channels, and when you want to explore multiple granularities from one fit rather than commit to a single k.