Some data doesn't just cluster; it evolves. Hierarchical clustering reveals the nested relationships within your dataset through the power of tree structures.
1Bottom-Up Merges
Agglomerative Clustering is the most common approach. It starts with every single data point as a cluster of one. It then iteratively finds the two closest clusters and merges them into a larger group. This continues until only one giant cluster remains.
2Visualizing the Tree
The Dendrogram is a specialized plot that shows every single merge operation. By looking at the heights of the horizontal lines, you can tell how dissimilar clusters were before merging. Longer vertical lines indicate more distinct separation between groups.
3Linkage Methods
How do we measure the distance between clusters? Linkage defines the rule. Ward's method minimizes the variance of merged clusters, while Single linkage uses the distance between the two closest points. Choosing the right linkage is critical for accurate clustering.
4Step-by-Step Breakdown
Hierarchical Clustering creates a tree of data. There are two main types: Agglomerative (Bottom-Up) and Divisive (Top-Down).
Let's use Agglomerative Clustering from Scikit-Learn. It starts with every point as its own cluster and merges them iteratively.
Checkpoint: What is the approach called that starts with all points in one cluster and splits them recursively?
- āAgglomerative
- āDivisive
We can visualize this merge history using a Dendrogram. The vertical axis represents the distance between merged clusters.
'Ward' is a popular linkage method that minimizes the variance of the clusters being merged.
Choosing the number of clusters involves 'cutting' the dendrogram at a specific height. A horizontal line across the longest vertical branches is usually best.
Checkpoint: In a Dendrogram, what does the vertical distance between two merged nodes represent?
- āDistance/Dissimilarity
- āNumber of Samples
Excellent! You've successfully mapped the data tree. Hierarchical clustering is invaluable for taxonomy and social network analysis.
Run Real Agglomerative Clustering. Finish clustering two well-separated point pairs and confirm the resulting labels.
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Describe Dendrogram Structure in Text
A dendrogram's meaning comes from its branch heights and merge order, which is entirely visual ā accompany any dendrogram shown on a page with a text summary of the key finding ('Two dominant clusters emerge at a distance of 15, further splitting into 4 sub-groups below distance 8'), since a screen reader cannot convey tree geometry.
<p>Two dominant clusters merge at distance 15; each splits into 2 sub-clusters below distance 8.</p>SEO Implications
- 1
A Dendrogram Is a Rendered Image, Not Structured Page Data
The linkage matrix and resulting dendrogram plot exist only as computed output in a script or notebook ā this page's SEO value rests on its own explanation of agglomerative clustering and linkage methods, not on any specific dendrogram's shape.
Best Practices
Compare Multiple Linkage Methods Before Committing to One
Ward, single, complete, and average linkage can produce meaningfully different cluster shapes on the same data ā Ward tends to produce balanced, similarly-sized clusters while single linkage can produce elongated 'chains'. Try a few before assuming the default is correct for your data's actual structure.
Use the Dendrogram Itself to Choose K, Not an Arbitrary Guess
Rather than guessing a cluster count upfront (as K-Means requires), let the dendrogram inform the decision ā look for the longest vertical gap without any horizontal merge line crossing it, and cut there. This data-driven approach is one of hierarchical clustering's key advantages over K-Means.
Frequent Bugs
Forgetting to scale features before computing a linkage matrix, just like with K-Means or PCA.
Hierarchical clustering computes distances between points, so unscaled features with different numeric ranges will dominate the distance calculation exactly as they would in K-Means or KNN. Always apply StandardScaler (or similar) before calling linkage() or fitting AgglomerativeClustering.
Real-World Examples
Building a Product Taxonomy from Purchase Data
An e-commerce catalog team uses hierarchical clustering on product co-purchase patterns to automatically discover a nested category taxonomy ā the dendrogram naturally reveals that 'running shoes' and 'hiking boots' merge into a broader 'athletic footwear' cluster before merging further into all of 'footwear', mirroring how a human-designed taxonomy would be structured.
Z = linkage(product_features, method='ward')
dendrogram(Z, labels=product_names, truncate_mode='level', p=4)