How does Hierarchical Clustering work?

Authors
How does Hierarchical Clustering work?

Hierarchical clustering builds groups step by step and arranges them in a tree, called a dendrogram, that goes from small groups to one big group.

In this blog, we will learn about how Hierarchical Clustering works. We will also see how it merges the closest groups step by step, how the dendrogram shows these merges as a tree, how we measure the distance between two groups, and how we cut the tree to choose the number of clusters.

We will cover the following:

  • What is hierarchical clustering?
  • The step-by-step merging process
  • The dendrogram, a tree of clusters
  • How we measure distance between clusters
  • Cutting the tree to choose clusters

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What is hierarchical clustering?

Clustering means grouping similar items together, without anyone telling us the groups in advance.

Hierarchical clustering builds groups step by step, and arranges them in a tree, from small groups to one big group.

It does not just give us flat groups. It shows us how the groups connect at every level.

There are two ways to build this tree.

  • Bottom-up. We start with each item as its own tiny group, then keep merging the closest groups until only one big group is left. This is the common way.
  • Top-down. We start with one big group holding everything, then keep splitting it into smaller groups.

We will focus on the bottom-up way, because it is the most used.

The step-by-step merging process

The best way to learn this is by taking an example.

Let's say we have five points on a line, named A, B, C, D, and E, at these positions.

   A     B          C     D              E
   |     |          |     |              |
   1     2          5     6             12

Now, let's build the tree by merging the closest pair, one step at a time.

Step 1: Every point starts as its own group. So we have five groups: A, B, C, D, E.

Step 2: We find the two closest groups. A is at 1 and B is at 2, so they are only 1 apart. C and D are also 1 apart. Let's merge A and B first into a group (AB).

Step 3: Now we look again. C and D are 1 apart, which is the closest. We merge them into (CD).

Step 4: Now we look again. Here, we measure the distance between two groups by their farthest points. The farthest points of (AB) and (CD) are 1 and 6, so they are 5 apart. The farthest points of (CD) and E are 5 and 12, so they are 7 apart. (AB) and (CD) are the closest remaining pair, so we merge them into (ABCD).

Step 5: Only E is left, sitting far away at 12. Its farthest point in (ABCD) is A at 1, so they are 11 apart. We merge it in last to form one big group (ABCDE).

Here, we can notice the pattern. We always merge the closest two groups, and we keep going until everything is joined into one. Points that are close join early, and points that are far join late.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

The dendrogram, a tree of clusters

The whole process is drawn as a tree called a dendrogram. The height of each join shows how far apart the two groups were when they merged. A low join means very similar. A high join means very different.

   height
     |
  11 |                _____________________
     |               |                     |
   5 |        _______|_______              |
     |       |               |             |
   1 |    ___|___         ___|___          |
     |   |       |       |       |         |
     |   A       B       C       D         E

Let's read this dendrogram. A and B join low down, at height 1, because they are very close. C and D also join at height 1. Then the (AB) and (CD) groups join higher up, at height 5, because they are farther apart. Finally, E joins at the very top, at height 11, because it is far from everyone.

So, the dendrogram tells us not just the groups, but also how close or far every group is from the others.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

How we measure distance between clusters

There is one detail we skipped. When a group has many points, how do we measure the distance between two groups? This choice is called the linkage.

There are a few common ways.

  • Single linkage. The distance is the gap between the two closest points, one from each group.
  • Complete linkage. The distance is the gap between the two farthest points, one from each group. This is the one we used in our example.
  • Average linkage. The distance is the average gap between all pairs of points across the two groups.
   Group 1: o o o          Group 2: x x x

   Single linkage   -> distance between the nearest o and x
   Complete linkage -> distance between the farthest o and x
   Average linkage  -> average of all o-to-x distances

Each choice gives slightly different shaped clusters. We pick the one that fits our data best.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

Cutting the tree to choose clusters

Now, the next question. The tree joins everything into one big group at the top. But we usually want a few groups, not one. So how do we get them?

We cut the tree with a horizontal line at some height. Every branch the line crosses becomes one cluster.

   height
     |
  11 |                _____________________
     |               |                     |
   5 |        _______|_______              |
     |       |               |             |
   3 |- - - -|- - - - - - - -|- - - - - - -|-
   1 |    ___|___         ___|___          |
     |   |       |       |       |         |
     |   A       B       C       D         E

Here, we cut at a height of 3, between 1 and 5. The cut line crosses three vertical lines, the (AB) stem, the (CD) stem, and E's line. So it separates the tree into three clusters: (AB), (CD), and (E). If we cut lower, we get more clusters. If we cut higher, we get fewer clusters.

So, the big advantage of hierarchical clustering is that we do not have to decide the number of clusters in advance. We build the whole tree first, then choose the number by picking where to cut.

We have a detailed blog on K-Means Clustering that explains the other popular method, where we choose the number of clusters up front.

Frequently Asked Questions

What is the difference between bottom-up and top-down hierarchical clustering?

Bottom-up starts with each item as its own tiny group and keeps merging the closest groups until only one big group is left. Top-down does the opposite. It starts with one big group holding everything and keeps splitting it into smaller groups. Bottom-up is the common and most used way.

Do we need to choose the number of clusters before running hierarchical clustering?

No. We do not have to decide the number of clusters in advance. We build the whole tree first, then choose the number of clusters by picking the height where we cut it. Cutting lower gives more clusters, and cutting higher gives fewer clusters.

What does the height of a join in a dendrogram mean?

The height of a join shows how far apart the two groups were when they merged. A low join means the two groups are very similar. A high join means they are very different. So, the dendrogram tells us not just the groups, but also how close or far every group is from the others.

Which linkage should we use in hierarchical clustering?

There is no single best linkage. Single linkage uses the two closest points, complete linkage uses the two farthest points, and average linkage uses the average gap between all pairs of points across the two groups. Each choice gives slightly different shaped clusters, so we pick the one that fits our data best.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.