Hierarchical Clustering: A Comprehensive Guide
Clustering is one of the most fundamental tasks in unsupervised machine learning, with the goal of discovering natural groupings or structures within a dataset. While there are many different clustering algorithms to choose from, hierarchical clustering stands out for its ability to capture the intrinsic hierarchical relationships between data points. In this article, we‘ll take an in-depth look at hierarchical clustering, exploring its inner workings, comparing it to other popular methods like k-means, and walking through some code examples. By the end, you‘ll have a solid understanding of when and how to leverage this powerful technique in your own projects.
What is Hierarchical Clustering?
Hierarchical clustering is a family of clustering algorithms that aim to build a hierarchy of clusters, where each cluster node contains child clusters. This hierarchical structure allows us to see the relationships between individual data points as well as higher-level groupings. There are two main approaches to hierarchical clustering:
- Agglomerative (bottom-up): Each data point starts in its own cluster, and pairs of clusters are iteratively merged moving up the hierarchy.
- Divisive (top-down): All data points start in one cluster, and recursive splits are performed moving down the hierarchy.
In both cases, we end up with a tree-like diagram called a dendrogram that shows the hierarchical relationships between clusters. The leaf nodes represent individual data points, while the internal nodes represent the merged clusters. By cutting the dendrogram at a given height, we can obtain a clustering at a selected precision.

Agglomerative Hierarchical Clustering
Agglomerative clustering is the most common hierarchical approach, so let‘s walk through its steps in more detail:
- Assign each data point to its own cluster.
- Compute the proximity matrix containing the pairwise distances between clusters.
- Find the pair of clusters with the smallest distance and merge them into a single cluster.
- Update the proximity matrix to reflect the merge operation.
- Repeat steps 3-4 until all the data points are in a single cluster.
There are several ways to measure the distance between two clusters, known as linkage criteria:
- Single linkage: the minimum distance between any two members of the two clusters
- Complete linkage: the maximum distance between any two members of the two clusters
- Average linkage: the average distance between all pairs of members from the two clusters
- Centroid linkage: the distance between the centroids of the two clusters
- Ward‘s linkage: the sum of squared differences within all clusters (minimizes variance)
Each linkage criterion has its own characteristics and will lead to different cluster assignments. Single linkage tends to produce long, trailing clusters, while complete linkage has a preference for compact, tightly bound clusters. Ward‘s method is often preferred since it minimizes the total within-cluster variance.
Here‘s a snippet of Python code showing how to perform agglomerative clustering using SciPy:
from scipy.cluster.hierarchy import dendrogram, linkage
from matplotlib import pyplot as plt
X = [[1, 1], [1.5, 1], [3, 3], [4, 4], [3, 3.5], [3.5, 4]]
labels = range(1, 7)
Z = linkage(X, ‘ward‘)
fig = plt.figure(figsize=(10, 5))
dn = dendrogram(Z, labels=labels)
plt.title(‘Hierarchical Clustering Dendrogram‘)
plt.xlabel(‘Data Points‘)
plt.ylabel(‘Distance‘)
plt.show()
This will generate a dendrogram visualizing the hierarchical structure of the clusters.
Comparing Hierarchical Clustering to K-Means
K-means is another popular clustering algorithm that aims to partition n data points into k clusters, where each data point belongs to the cluster with the nearest mean. While both hierarchical clustering and k-means can be used to group similar data points together, there are some key differences to consider:
- Hierarchical clustering outputs a tree-based representation of the data, while k-means just returns the final cluster assignments.
- Hierarchical clustering does not require specifying the number of clusters upfront, while k-means does.
- The time complexity of hierarchical clustering is at least quadratic in the number of data points, while k-means is linear.
- Hierarchical clustering is more sensitive to noise and outliers than k-means.
In general, hierarchical clustering is a good choice when you don‘t know the optimal number of clusters and want to capture the intrinsic structure of the data. It can also handle non-isotropic clusters and non-convex shapes. On the other hand, k-means is more scalable to large datasets and tends to work well when the clusters are roughly spherical.
Here‘s a comparison of the two methods on the classic Iris dataset using scikit-learn:
from sklearn.cluster import AgglomerativeClustering, KMeans
from sklearn.datasets import load_iris
from sklearn.metrics import adjusted_rand_score
iris = load_iris()
X = iris.data
hierarchical = AgglomerativeClustering(n_clusters=3)
kmeans = KMeans(n_clusters=3, random_state=0)
h_labels = hierarchical.fit_predict(X)
k_labels = kmeans.fit_predict(X)
print("Hierarchical ARI: {:.3f}".format(adjusted_rand_score(iris.target, h_labels)))
print("K-Means ARI: {:.3f}".format(adjusted_rand_score(iris.target, k_labels)))
Hierarchical ARI: 0.731
K-Means ARI: 0.730
As we can see, both methods achieve similar performance on this dataset. The adjusted Rand index (ARI) is an external evaluation metric that measures the similarity between the true labels and the cluster assignments, with a value close to 1.0 indicating high agreement.
Choosing the Optimal Number of Clusters
One of the challenges with hierarchical clustering is deciding where to cut the dendrogram to obtain the final clustering. There are a few different approaches:
- Domain knowledge: Choose a number of clusters that aligns with your understanding of the data.
- Elbow method: Plot the total within-cluster sum of squares (WSS) against the number of clusters, and look for a bend or elbow in the curve.
- Silhouette analysis: Calculate the average silhouette coefficient for different numbers of clusters, and choose the one that maximizes the score.
- Gap statistic: Compare the WSS to its expected value under a reference null distribution.
Here‘s an example of using the silhouette coefficient to select the optimal number of clusters:
from sklearn.metrics import silhouette_score
n_clusters = [2, 3, 4, 5, 6]
for n in n_clusters:
hierarchical = AgglomerativeClustering(n_clusters=n)
cluster_labels = hierarchical.fit_predict(X)
silhouette_avg = silhouette_score(X, cluster_labels)
print("For n_clusters =", n, "The average silhouette_score is:", silhouette_avg)
For n_clusters = 2 The average silhouette_score is: 0.681
For n_clusters = 3 The average silhouette_score is: 0.553
For n_clusters = 4 The average silhouette_score is: 0.498
For n_clusters = 5 The average silhouette_score is: 0.488
For n_clusters = 6 The average silhouette_score is: 0.361
We can see that n=2 achieves the highest silhouette score, so that would be the optimal choice according to this metric.
Advantages and Disadvantages
Advantages of hierarchical clustering:
- Outputs a hierarchy of clusters, which can reveal interesting structure in the data
- Does not require specifying the number of clusters upfront
- Can handle clusters with non-convex shapes and different sizes
- Provides deterministic results (no randomness involved)
Disadvantages of hierarchical clustering:
- Has a time complexity of O(n^3) in the general case, making it impractical for large datasets
- Sensitive to noise and outliers in the data
- Greedy algorithms can sometimes get stuck in local optima
- The choice of distance metric and linkage criterion can have a strong effect on the results
Applications of Hierarchical Clustering
Hierarchical clustering is used in a wide variety of domains, including:
- Biology: Classification of genes and proteins
- Social networks: Detecting communities and influential figures
- Market segmentation: Identifying groups of customers with similar needs and behaviors
- Image segmentation: Partitioning images into meaningful regions
- Anomaly detection: Finding rare or unusual data points
Whenever you have a dataset with an underlying hierarchical structure, this algorithm can be a powerful tool for uncovering meaningful patterns and relationships.
Advanced Topics and Extensions
There are several variations and extensions of hierarchical clustering that are worth mentioning:
- Constrained hierarchical clustering: Allows incorporating must-link and cannot-link constraints between data points
- Model-based hierarchical clustering: Assumes the data is generated from a mixture of probabilistic models
- Hierarchical density-based clustering: Combines hierarchical clustering with density-based methods like DBSCAN
- Incremental hierarchical clustering: Enables efficient updates of the hierarchy as new data points arrive
Researchers are actively exploring ways to scale up hierarchical methods to larger datasets and incorporate prior knowledge into the clustering process. Some exciting directions include using distributed computing frameworks, subsampling techniques, and incorporating deep learning.
Frequently Asked Questions
Q: When should I use hierarchical clustering vs k-means?
A: Hierarchical clustering is a good choice when you don‘t know the optimal number of clusters upfront, want to capture the intrinsic structure of the data, and have relatively small datasets. K-means is better suited for larger datasets, when you have a pre-specified number of clusters, and when the clusters are expected to be roughly spherical.
Q: How do I interpret a dendrogram?
A: A dendrogram is a tree-like diagram that shows the hierarchical structure of the clusters. The height of each node represents the distance between its child clusters. By cutting the dendrogram at a given height, you can obtain a clustering at a selected level of granularity.
Q: What are some common evaluation metrics for hierarchical clustering?
A: Some popular external evaluation metrics (when ground truth labels are available) include adjusted Rand index, mutual information, and purity. Internal evaluation metrics (which don‘t require labels) include silhouette coefficient, Calinski-Harabasz index, and Davies-Bouldin index. It‘s important to choose a metric that aligns with your specific goals and assumptions.
Q: How can I make hierarchical clustering more scalable?
A: Some strategies for scaling up hierarchical clustering include using approximate nearest neighbor search, subsampling the data, and leveraging distributed computing frameworks like Apache Spark. There are also some variants like BIRCH and CURE that are specifically designed for large datasets. However, hierarchical methods will always be more computationally expensive than partitional methods like k-means.
Conclusion
Hierarchical clustering is a powerful and versatile unsupervised learning technique that can uncover meaningful structures in your data. By building a hierarchy of clusters, it allows you to explore relationships at multiple levels of granularity and does not require specifying the number of clusters upfront. However, its quadratic time complexity can be a limiting factor for large datasets.
When deciding between hierarchical clustering and k-means, consider your specific problem requirements and the characteristics of your data. If you have a relatively small dataset, need to capture hierarchical structure, and don‘t know the optimal number of clusters, hierarchical clustering may be the better choice. On the other hand, if you have a very large dataset, a pre-specified number of clusters, and expect roughly spherical clusters, k-means is likely to be more efficient and effective.
Whichever method you choose, be sure to experiment with different distance metrics, linkage criteria, and evaluation techniques to get the most informative results. With a solid understanding of the strengths and limitations of each approach, you‘ll be well-equipped to tackle a wide range of real-world clustering problems.