Hierarchical Clustering: Building Clusters Like a Family Tree
Master agglomerative clustering and dendrograms. Learn linkage methods (Ward, complete, average, single) and how to choose the optimal number of clusters by cutting the tree at the right height.
Hierarchical Clustering: Building Clusters Like a Family Tree
Understand how data groups emerge by merging or splitting, visualized with dendrograms.
Hierarchical Clustering: Building Clusters Like a Family Tree
Imagine organizing items not just into separate boxes (like K-Means does), but creating a structure showing how groups merge into larger groups, like a family tree or an organizational chart. This is the idea behind Hierarchical Clustering, another powerful unsupervised learning technique for finding patterns and groups in data.
Unlike K-Means where you must specify the number of clusters beforehand, Hierarchical Clustering builds a hierarchy of clusters, allowing you to explore different levels of grouping afterward. Today, we’ll focus on the most common approach: Agglomerative Hierarchical Clustering.
Main Technical Concept: Hierarchical Clustering creates a nested sequence of clusters. Agglomerative (bottom-up) clustering starts with each data point as its own cluster and iteratively merges the two closest clusters until only one cluster (containing all data) remains. The process creates a tree structure called a dendrogram.
Two Ways to Build the Hierarchy
There are two main strategies for hierarchical clustering:
- Agglomerative (Bottom-Up): This is the approach we’ll focus on.
- Starts with each data point in its own cluster (N clusters).
- Finds the two closest clusters and merges them.
- Repeats the merging process until only one single cluster remains.
- Think: Building up from individuals to families to extended families…
- Divisive (Top-Down): Less common.
- Starts with all data points in one single cluster.
- Recursively splits the most heterogeneous cluster into two smaller ones.
- Repeats until each data point is in its own cluster or a stopping criterion is met.
- Think: Starting with the whole population and dividing it into countries, then states, then cities…
We will primarily discuss the Agglomerative method as it’s more widely used and implemented.
The Agglomerative Clustering Process: Step-by-Step
Here’s how the bottom-up merging works:
- Start Separate: Treat each data point as its own individual cluster. If you have N data points, you start with N clusters.
- Find Closest Pair: Calculate the distance (or dissimilarity) between every possible pair of clusters currently existing. Identify the two clusters that are closest to each other according to a chosen metric.
- Merge: Combine these two closest clusters into a single new cluster. You now have one fewer cluster than before.
- Repeat: Go back to Step 2. Recalculate distances between all current clusters (including the newly formed one) and find the next closest pair to merge.
- Stop: Continue this process until all data points belong to a single, large cluster.
This merging process naturally creates a hierarchy, which we can visualize.
Key Ingredients: Measuring Distance & Linking Clusters
To decide which clusters to merge, we need two crucial components:
1. Distance Metric: How Close are Points?
First, we need a way to measure the distance or dissimilarity between individual data points. Common choices for numerical data include:
- Euclidean Distance: The straight-line distance (most common).
- Manhattan Distance: The “city block” distance.
- Correlation Distance, Cosine Similarity, etc. (depending on the data type and problem).
Just like K-Means, Hierarchical Clustering using these metrics is sensitive to feature scales. Feature Scaling (Standardization/Normalization) is generally recommended before clustering!
2. Linkage Method: How Close are Clusters?
Once we have multiple points in a cluster, how do we measure the distance between two clusters? This is defined by the Linkage Criterion:
- Single Linkage (Minimum Linkage): The distance between two clusters is the distance between the closest pair of points, one from each cluster. Can lead to long, “chain-like” clusters.
- Complete Linkage (Maximum Linkage): The distance between two clusters is the distance between the farthest pair of points, one from each cluster. Tends to produce more compact, spherical clusters.
- Average Linkage: The distance between two clusters is the average distance between all possible pairs of points, one from each cluster. A balance between single and complete linkage.
- Ward’s Linkage: (Very Common & Often Recommended) It doesn’t just use pairwise distances. Instead, it merges the pair of clusters that leads to the minimum increase in the total within-cluster variance after merging. It tries to find compact, spherical clusters by minimizing the variance within each cluster.
The choice of linkage method can significantly impact the final structure of the clusters!
Visualizing the Hierarchy: The Dendrogram
The entire merging process of agglomerative clustering can be beautifully visualized using a tree-like diagram called a Dendrogram.
- Structure: It looks like an upside-down tree. The leaves at the bottom represent the individual data points.
- Merging: As you move up, horizontal lines connect branches, indicating that two clusters (or points) were merged.
- Height of Merge: The vertical height of the horizontal connecting line represents the distance (or dissimilarity) at which those two clusters were merged according to the chosen linkage criterion. Longer vertical lines mean the clusters merged were further apart.
Dendrogram Example (5 data points: A, B, C, D, E)
Distance
│
5 │ ┌──────────────────┐
│ │ │
3 │ ┌────────┤ ┌────────┤
│ │ │ │ │
2 │ ┌─────┤ │ ┌────┤ │
│ │ │ │ │ │ │
1 │ ┌──┤ │ │ ┌──┤ ...
│ │ │ │ │ │ │
└──┴──┴─────┴────────┴─┴──┴──────────────
A B C D E
To get 2 clusters → cut at height ~3
Cluster 1: {A, B, C} | Cluster 2: {D, E}
To get 3 clusters → cut at height ~2
Cluster 1: {A, B} | Cluster 2: {C} | Cluster 3: {D, E}
Dendrograms are extremely useful because they show the entire hierarchy of merges, allowing us to choose the number of clusters after the algorithm has run!
Choosing the Number of Clusters (K) from the Dendrogram
Unlike K-Means, you don’t have to specify K beforehand. You can decide by looking at the dendrogram:
- Identify the longest vertical line(s) that don’t cross any horizontal merge lines. These represent the largest distances between merges.
- Draw a horizontal line across the dendrogram that cuts through these longest vertical lines.
- The number of vertical lines this horizontal line intersects is a good suggestion for the optimal number of clusters (K).
Essentially, you are “cutting” the tree at a height that represents a significant jump in distance required to merge clusters, suggesting that the groups below that cut are relatively distinct.
Advantages and Disadvantages of Hierarchical Clustering
Pros:
- No Need to Pre-specify K: The biggest advantage! The dendrogram allows you to explore different numbers of clusters after the algorithm runs.
- Provides Hierarchy: The dendrogram visualizes the relationship and similarity between clusters, which can be insightful.
- Works Well with Small Datasets: Can be effective where K-Means might struggle with initialization.
- Intuitive Visualization: Dendrograms are relatively easy to understand.
Cons:
- Computationally Expensive: Calculating all pairwise distances can be slow, especially for large datasets (often O(n³) or O(n²) depending on implementation and linkage).
- Memory Intensive: Typically requires storing the distance matrix.
- Sensitivity to Distance/Linkage: The results can vary significantly based on the chosen distance metric and linkage criterion.
- Noisy Dendrograms: Can be hard to interpret clearly for large datasets.
- Irreversible Merges: Once two clusters are merged in the agglomerative process, they cannot be undone later, which might lead to suboptimal global solutions sometimes.
- Doesn’t Scale Well: Generally less suitable for very large datasets compared to K-Means or DBSCAN.
Tips for Effective Hierarchical Clustering
Best Practices:
- Scale Your Features: Crucial if using distance metrics like Euclidean.
- Choose Linkage Wisely: ‘Ward’ linkage is often a good default as it tries to minimize variance within clusters. Experiment with others (average, complete) if Ward doesn’t yield good results for your data structure.
- Interpret the Dendrogram Carefully: Use the “longest vertical line” heuristic as a guide for choosing K, but also consider the context of your problem and potentially other metrics (like Silhouette score applied post-hoc for different K cuts).
- Consider Dataset Size: Be aware of the computational cost. For very large datasets, hierarchical clustering might be too slow or memory-intensive.
- Visualize Results: After choosing K and assigning cluster labels, visualize the clusters using scatter plots (for 2D/3D data) or other techniques to see if they make sense.
Hierarchical Clustering: Key Takeaways
- Hierarchical Clustering builds a tree-like hierarchy of clusters without needing K specified upfront.
- Agglomerative (Bottom-Up) starts with individual points and iteratively merges the closest clusters.
- Requires choosing a Distance Metric (e.g., Euclidean) and a Linkage Method (e.g., Ward, Average, Complete) to define cluster distance.
- The result is visualized as a Dendrogram, which shows the merge history and distances.
- The optimal number of clusters (K) can often be determined by “cutting” the dendrogram at an appropriate height.
- Feature Scaling is generally recommended.
- Main Pros: No need to pre-specify K, provides hierarchical structure.
- Main Cons: Computationally expensive for large datasets, sensitive to linkage choice.