Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 59

Interpreting Hierarchical Clustering and Dendrograms

Recommended resources

Hello! Welcome to your next lesson in our Unsupervised Learning module.

In our last two lessons, we explored k-means clustering. A key challenge we addressed was selecting the number of clusters, K, before running the algorithm, using methods like the elbow plot and silhouette scores. Today, we'll explore a different approach that removes this requirement.

This lesson introduces hierarchical clustering, a method that builds a tree-like hierarchy of clusters. Our goal is to apply hierarchical clustering and interpret the resulting dendrogram. You will learn how the algorithm works conceptually, what a dendrogram represents, and how to use this powerful visualization to understand the structure of your data and select an appropriate number of clusters.

1. What is Hierarchical Clustering?

Unlike k-means, which produces a single "flat" partitioning of the data for a given K, hierarchical clustering creates a multi-level structure of clusters. There are two main approaches:

  • Agglomerative (Bottom-Up): This is the most common method. It starts by treating each data point as its own cluster and then progressively merges the two closest clusters until only one cluster (containing all points) remains.
  • Divisive (Top-Down): This method starts with all data points in a single cluster and recursively splits them until each point is its own cluster.

We will focus on the agglomerative approach, as it's more widely used in practice.

The result of hierarchical clustering is a structure that is very intuitive to visualize. Let's look at an image that connects the raw data points to their clustered hierarchy.

Hierarchical Clustering Dendrogram Example
This image shows seven data points in a 2D space on the left. On the right is the corresponding dendrogram, a tree diagram that illustrates how these points are grouped together. Notice how points that are close in the scatter plot (like P1 and P2) are the first to merge in the tree.

2. The Agglomerative Clustering Process

The "bottom-up" logic of agglomerative clustering is straightforward. Let's walk through the steps conceptually.

  1. Initialize: Start with each data point as a separate cluster.
  2. Find Closest Pair: Identify the two clusters that are "closest" to each other based on a chosen distance measure.
  3. Merge: Combine these two clusters into a new, single cluster.
  4. Repeat: Repeat steps 2 and 3 until all data points have been merged into one large cluster.

To see this process in action, the following video provides an excellent visual explanation using a simple heatmap example.

StatQuest: Hierarchical Clustering

This video from StatQuest with Josh Starmer demonstrates the core idea of hierarchical clustering and how it progressively groups similar items.

Watch from the beginning to 04:38. Focus on the conceptual steps: how individual items (genes, in this case) are compared, how the most similar pair is merged into a cluster, and how this new cluster is then compared with the remaining items in the next iteration.

This process naturally raises a critical question: what exactly do we mean by "closest"? Answering this involves two key choices: the distance metric and the linkage criterion.


3. Defining "Closest": Distance Metrics and Linkage Criteria

To implement the algorithm, we need to precisely define how we measure the distance between clusters.

Distance Metric: Measuring the Distance Between Points

First, we need a way to measure the distance between two individual data points. The most common choice is the Euclidean distance, which you can think of as the straight-line distance between two points. Given your engineering background, you'll recognize this as a direct application of the Pythagorean theorem, generalized to multiple dimensions.

Linkage Criterion: Measuring the Distance Between Clusters

Once we have clusters with more than one point, how do we measure the distance between them? This is determined by the linkage criterion. There are several methods, and the choice can significantly impact the final cluster structure. The most common ones are:

  • Single Linkage: The distance between two clusters is the distance between the closest pair of points across the two clusters.
  • Complete Linkage: The distance is determined by the farthest pair of points across the two clusters.
  • Average Linkage: The distance is the average of all pairwise distances between points in the two clusters.
  • Ward's Method: This method merges the pair of clusters that leads to the minimum increase in the total within-cluster variance. It tends to produce compact, similarly-sized clusters and is a very popular default choice.

The following video from StatQuest clearly explains both distance metrics and linkage criteria.

StatQuest: Hierarchical Clustering

Let's continue with the StatQuest video. This part details how 'similarity' is quantified using distance metrics and how different linkage methods compare clusters.

Watch from 05:15 to 11:02. Pay attention to the geometric interpretation of Euclidean vs. Manhattan distance. Then, focus on understanding the visual difference between single, complete, and centroid (average) linkage.

As you saw, your choice of distance and linkage is not trivial; it influences the shape and size of the clusters the algorithm identifies.


4. Interpreting the Dendrogram

The primary output of hierarchical clustering is the dendrogram. This tree diagram visualizes the entire merging process and is the key to interpreting the results.

How to Read a Dendrogram

  • The leaves at the bottom are the individual data points.
  • As you move up, horizontal lines indicate a merge.
  • The vertical position (height) of the horizontal line indicates the distance (or dissimilarity) at which the two clusters were merged. Longer vertical lines mean that two clusters were merged at a greater distance, implying they are less similar.

The following video provides an excellent walkthrough of interpreting a dendrogram using a real-world example.

Flat and Hierarchical Clustering | The Dendrogram Explained

This video from 365 Data Science clearly explains what a dendrogram is and how to use it to understand cluster relationships and select a final number of clusters.

Watch from 03:06 to 07:02. Focus on how the hierarchy is read from bottom to top, how the vertical distance between merges signifies dissimilarity, and the simple 'cutting' method for choosing a number of clusters.

Choosing the Number of Clusters

As the video explained, one of the main advantages of hierarchical clustering is that you don't need to pre-specify the number of clusters. Instead, you can decide on the number of clusters after seeing the full hierarchy in the dendrogram. You do this by making a horizontal "cut" across the tree.

Dendrogram for Hierarchical Clustering
This dendrogram shows how a horizontal cut (the red dashed line) determines the number of clusters. The number of vertical lines the cut intersects equals the number of resulting clusters. Here, the cut intersects two main branches, resulting in two clusters.

A common heuristic for deciding where to cut is to look for the longest vertical line that is not intersected by any horizontal merge lines. Cutting through this line often represents a good separation point because it corresponds to merging two very dissimilar groups.

The following reading explains this practical rule with clear illustrations.

Finding number of cluster using Dendogram

This article from 'Mastering Hierarchical Clustering' provides a step-by-step guide for using a dendrogram to find the optimal number of clusters.

Read the section 'Finding number of cluster using Dendogram'. Focus on the rule about identifying the longest vertical line and drawing a horizontal cut through it. Study the two examples provided to solidify your understanding.

Test your understanding!

Look at the Hierarchical Clustering Dendrogram image we saw at the beginning of the lesson.

  1. Which two points were the very first to be merged into a cluster?
  2. Based on the dendrogram, which is more similar: the pair (P1, P2) or the pair (P6, P7)? Why?
  3. If you wanted to have 3 clusters, where would you make a horizontal cut? Which points would be in each cluster?
Show answer
  1. The points P1 and P2 were the first to be merged. Their horizontal merge line is the lowest on the dendrogram, indicating the smallest distance.
  2. The pair (P1, P2) is more similar than (P6, P7). The merge height for (P1, P2) is lower than the merge height for (P6, P7), signifying less distance between them.
  3. To get 3 clusters, you would make a horizontal cut at a height of around 3.5. This would intersect three vertical lines, yielding the following clusters:
    • Cluster 1: {P1, P2, P3}
    • Cluster 2: {P4, P5}
    • Cluster 3: {P6, P7}

Conclusion

Today you've learned a powerful and intuitive clustering method that complements k-means. Hierarchical clustering provides a rich, visual representation of your data's structure, freeing you from having to guess the number of clusters upfront.

Key Takeaways:

  • Hierarchical Clustering builds a nested hierarchy of clusters, most commonly using an agglomerative (bottom-up) approach.
  • The algorithm's behavior is defined by the distance metric (e.g., Euclidean) used for points and the linkage criterion (e.g., Ward's, Complete, Single) used for clusters.
  • The dendrogram is a tree diagram that visualizes the entire merging process. The height of each merge represents the dissimilarity between the merged clusters.
  • You can determine the final number of clusters by making a horizontal cut across the dendrogram. A good place to cut is often across the longest vertical line that doesn't cross a merge line.

Preview of the Next Lesson:

So far, both k-means and hierarchical clustering assign each data point to exactly one cluster (this is called "hard clustering"). But what if a data point could belong to multiple clusters with different probabilities? In our next lesson, we will explore Gaussian Mixture Models (GMMs), a probabilistic method that provides a "soft clustering" by calculating the likelihood that each point belongs to each cluster.