ML Notes
Complete guide to hierarchical clustering covering agglomerative and divisive approaches, linkage methods, dendrograms, and practical implementation with scipy.
Hierarchical clustering creates a tree-like structure (called a dendrogram) that shows how data points progressively merge into larger clusters. Unlike K-Means where you must specify the number of clusters upfront, hierarchical clustering reveals the FULL clustering structure at all levels, letting you choose the granularity that makes sense for your problem. Want 3 broad clusters? Cut the tree at one level. Want 10 fine-grained clusters? Cut lower. This flexibility makes it invaluable for exploratory data analysis.
The Intuition: Family Trees for Data Points
Think of building a family tree, but instead of genealogy, you are grouping by similarity. Start with each data point as its own tiny cluster. Then repeatedly find the two most similar clusters and merge them into one. Keep merging until everything is in a single giant cluster. The order of these merges, visualized as a tree diagram, tells you exactly how the data is structured at every scale.
This bottom-up approach is called agglomerative clustering and is by far the most common variant. The opposite approach (divisive) starts with one big cluster and repeatedly splits it, but this is computationally more expensive and less commonly used.
How Agglomerative Clustering Works
The algorithm is elegantly simple:
- Initialize: Treat each data point as its own cluster (n clusters for n points)
- Find closest pair: Calculate distances between all pairs of clusters
- Merge: Combine the two closest clusters into one
- Record: Note which clusters merged and at what distance (this builds the dendrogram)
- Repeat: Go back to step 2 until only one cluster remains
The key question is: how do you define the "distance" between two clusters (not just two points)? This is where linkage methods come in.
Linkage Methods: Defining Cluster Distance
Different linkage methods produce dramatically different clustering results:
Single Linkage (Minimum): Distance between clusters = distance between their closest members. Tends to produce long, chain-like clusters. Good for elongated shapes but sensitive to noise.
Complete Linkage (Maximum): Distance between clusters = distance between their farthest members. Produces compact, spherical clusters. More robust to outliers than single linkage.
Average Linkage: Distance = average of all pairwise distances between members. A balanced compromise between single and complete linkage.
Ward's Method: Minimizes the increase in total within-cluster variance after merging. Produces the most compact, evenly-sized clusters. This is the most commonly used method and the default in most implementations.
| Single | min distance between any point in A and any point in B |
| Complete | max distance between any point in A and any point in B |
| Average | average of all pairwise distances between points in A and B |
| Ward | increase in total variance if A and B merge |
Implementation with Python
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
# Generate sample data
X, y_true = make_blobs(n_samples=200, centers=4, cluster_std=0.8, random_state=42)
X_scaled = StandardScaler().fit_transform(X)
# Method 1: scipy for dendrograms
Z = linkage(X_scaled, method='ward', metric='euclidean')
# Plot dendrogram
plt.figure(figsize=(12, 6))
dendrogram(Z, truncate_mode='lastp', p=30, leaf_rotation=90,
color_threshold=5.0)
plt.title('Hierarchical Clustering Dendrogram (Ward Linkage)')
plt.xlabel('Cluster Size')
plt.ylabel('Distance (Ward)')
plt.axhline(y=5.0, color='r', linestyle='--', label='Cut at distance 5.0')
plt.legend()
plt.tight_layout()
plt.show()
# Cut dendrogram at specific distance to get clusters
labels_scipy = fcluster(Z, t=4, criterion='maxclust') # exactly 4 clusters
print(f"Clusters from scipy: {len(set(labels_scipy))}")
# Method 2: sklearn (no dendrogram but integrates with sklearn pipeline)
agg = AgglomerativeClustering(n_clusters=4, linkage='ward')
labels_sklearn = agg.fit_predict(X_scaled)
score = silhouette_score(X_scaled, labels_sklearn)
print(f"Silhouette Score: {score:.4f}")Reading and Interpreting Dendrograms
The dendrogram is the most informative output of hierarchical clustering. Here is how to read it:
- The x-axis shows individual data points (or clusters at leaf level)
- The y-axis shows the distance at which merges occur
- Each horizontal line connects two clusters being merged
- The height of the horizontal line indicates how different the merged clusters were
- A large vertical gap suggests a natural cluster boundary — cut the tree there
The optimal number of clusters corresponds to cutting the dendrogram where the vertical lines are longest — these represent merges between genuinely different groups. Short vertical lines represent merges within natural clusters.
Hierarchical vs K-Means: Decision Guide
| Factor | Hierarchical | K-Means |
|---|---|---|
| Number of clusters | Choose after seeing dendrogram | Must specify upfront |
| Scalability | O(n³) — slow for large n | O(n×K×iter) — fast |
| Cluster shapes | Any (depends on linkage) | Spherical only |
| Deterministic | Yes (no random initialization) | No (depends on init) |
| Interpretability | Dendrogram shows structure | Centroids show centers |
| Best for | Small-medium data, exploration | Large data, production |
Practical Considerations and Tips
Hierarchical clustering has O(n²) memory requirements for the distance matrix and O(n³) time complexity for naive implementations, limiting it to datasets of roughly 10,000-50,000 points. For larger datasets, consider running K-Means first to reduce data to K centroids, then apply hierarchical clustering to those centroids — this gives you the dendrogram's interpretability with K-Means's scalability.
Always standardize features before hierarchical clustering since distances are scale-dependent. Ward's linkage is your best default choice — switch to other methods only if your clusters are known to be non-spherical (use single linkage) or you need robustness to outliers (use complete linkage).
Real-World Applications
Hierarchical clustering is widely used in bioinformatics for gene expression analysis (grouping genes with similar activity patterns), document clustering (organizing papers into topic hierarchies), customer segmentation (revealing natural customer tiers), taxonomy construction (biological classification), and social network analysis (community detection at multiple scales).
Advanced Techniques: Connectivity Constraints
In some applications, you want to ensure that only spatially adjacent points can merge. For example, in image segmentation, you want clusters to be contiguous regions, not scattered pixels. Sklearn supports connectivity constraints through a connectivity matrix that restricts which merges are allowed. This transforms hierarchical clustering from a purely distance-based method into a spatially-aware segmentation tool, particularly useful for geographic data and image analysis where spatial coherence matters. This capability distinguishes hierarchical clustering from purely centroid-based methods in domain-specific applications.
Key Takeaways
Hierarchical clustering builds a complete tree of similarities through progressive merging, letting you visualize clustering structure at all granularity levels via the dendrogram. Ward linkage with Euclidean distance is the robust default. Cut the dendrogram where vertical gaps are largest to find natural cluster boundaries. Use it for exploratory analysis on small-to-medium datasets where understanding the hierarchical structure is as important as the final cluster assignments.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Hierarchical Clustering.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, clustering, hierarchical, hierarchical clustering
Related Machine Learning Topics