Hierarchical Clustering

Hierarchical Clustering Explained: Build a Family Tree for Your Data

Have you ever grouped items without knowing how many groups you needed? That’s exactly what Hierarchical Clustering does for data. It builds a tree of merges, one step at a time, and lets you decide the number of clusters later.

This article is the companion piece to Episode 72 of the Intelevo YouTube series. If you’d rather watch the full walkthrough with visuals and a live code demo, check out the video first. Then come back here to review the concepts, the formula, and the code at your own pace.

Many learners find clustering intimidating at first. Numbers, distance formulas, and unfamiliar terms can pile up quickly. However, Hierarchical Clustering actually simplifies things once you see the underlying pattern. Instead of memorizing steps, you’ll recognize a process you already understand intuitively from everyday life.

By the end of this post, you’ll understand what Hierarchical Clustering is, how it works, and how to implement it in Python. Let’s get started.

What Is Hierarchical Clustering?

Hierarchical Clustering groups data by repeatedly combining the two closest points or groups. As a result, it builds a tree of merges. This tree starts with individual points and ends with one giant cluster at the top.

Here’s the best part. You don’t need to decide the number of clusters upfront. Instead, you look at the finished tree and choose your cut point afterward. This single feature separates Hierarchical Clustering from K-Means, where you must guess K before you even run the algorithm.

The Family Reunion Analogy

Let’s make this idea concrete. Picture a large family reunion. You don’t split everyone into groups by deciding a headcount first. Instead, the grouping happens naturally, step by step.

First, every person starts as an individual. Next, couples and siblings pair up because they’re closest to each other. Then, those small pairs combine into nuclear families. Finally, everyone connects into one large extended family at the top.

That’s the entire idea behind Hierarchical Clustering. You merge the two closest groups again and again until only one group remains. Afterward, you decide how many “families” you actually want by zooming back into the tree.

Two Directions: Agglomerative and Divisive

Hierarchical Clustering comes in two flavors. Understanding both will help you choose the right one for your project.

Agglomerative clustering works bottom-up. It starts with every point as its own cluster. Then, it merges the closest pair, again and again, until one cluster remains. This approach is the most common choice in real-world projects, and it’s the method we’ll use throughout this article.

Divisive clustering works top-down instead. It starts with everything in one cluster, then splits it repeatedly until each point stands alone. However, this method is less common because it’s more computationally expensive. For that reason, we’ll focus on agglomerative clustering going forward.

How Agglomerative Clustering Works

The agglomerative algorithm follows four simple steps. Once you understand these four steps, you understand the whole technique.

  1. Start separate. Treat every single data point as its own tiny cluster.
  2. Find the closest pair. Measure the distance between every pair of clusters.
  3. Merge them. Combine that closest pair into one new cluster.
  4. Repeat. Keep merging until only one cluster contains everything.

You simply loop steps two and three until nothing is left to merge. Nothing more complicated happens under the hood. This simplicity is exactly why the algorithm feels so intuitive once you see it in action.

Linkage Methods: Measuring Distance Between Groups

Here’s where a small complication appears. Once a cluster contains more than one point, how do you measure its distance to another cluster? This decision is called linkage, and it changes your results significantly.

There are four common linkage methods:

  • Single linkage uses the distance between the closest pair of points, one from each group.
  • Complete linkage uses the distance between the farthest pair of points, one from each group.
  • Average linkage averages every point-to-point distance between the two groups.
  • Ward’s method merges whichever pair increases the within-cluster variance the least.

So, which one should you pick? As a rule of thumb, Ward’s method works well as a default for most numeric datasets. It tends to produce compact, well-balanced clusters, which makes it a safe starting point for beginners.

Keep in mind that no single linkage method works best for every dataset. Therefore, it’s worth experimenting with two or three options and comparing the resulting dendrograms side by side. Often, the differences become obvious once you visualize them, and you’ll quickly notice which method captures your data’s natural structure.

Reading a Dendrogram

Once you run Hierarchical Clustering, you get a tree diagram called a dendrogram. This chart is the single most important visual in the entire technique, so let’s break it down.

Each leaf at the bottom represents one data point. As you move upward, lines join together every time two groups merge. Crucially, the height of each join tells you the distance at which that merge happened.

Therefore, you should always read a dendrogram from the bottom up. Lower merges mean the groups were very similar. Higher merges mean the groups were less alike. Eventually, every branch connects at the very top, forming one final cluster.

Choosing the Number of Clusters

Now we reach the payoff. Instead of guessing K before you start, you draw one horizontal line across the dendrogram. Then, you count how many vertical lines that line crosses. This count becomes your number of clusters.

If you cut higher up the tree, you get fewer, bigger clusters. If you cut lower down, you get more, smaller clusters. As a result, you get to make this decision only after seeing the full structure of your data, not before.

This approach removes the guesswork that often frustrates beginners using K-Means. Instead of trial and error, you make an informed decision based on a clear visual.

The Only Formula You Need

Despite all this structure, Hierarchical Clustering relies on one simple mathematical idea: straight-line distance between two points. This distance is called Euclidean distance, and you likely learned it in geometry class.

The formula looks like this:

d(A, B) = √[ (x₁ − x₂)² + (y₁ − y₂)² ]

Let’s trace through a quick example by hand. Suppose point A sits at coordinates (2, 3), and point B sits at coordinates (5, 7). First, subtract the x-values: 5 − 2 = 3. Next, subtract the y-values: 7 − 3 = 4. Then, square both results: 3² = 9, and 4² = 16. After that, add them together: 9 + 16 = 25. Finally, take the square root: √25 = 5.

So, the distance between points A and B equals 5. That’s genuinely all the math you need. Linkage methods, which we covered earlier, simply decide which of these distances to compare once a group contains more than one point.

Hierarchical Clustering in Python

Let’s move from theory to practice. We’ll build a dendrogram first, then cut it into real cluster labels using scikit-learn.

Step 1: Load the Data and Build the Dendrogram

import pandas as pd
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt

# Load your data (rows = samples, columns = features)
data = pd.read_csv("customers.csv")
X = data[["annual_income", "spending_score"]].values

# Build the merge tree — Ward linkage is a solid default
merged = linkage(X, method="ward")

# Draw the dendrogram
dendrogram(merged)
plt.show()

Let’s walk through this code. First, we import pandas to handle our dataset, along with dendrogram and linkage from scipy’s hierarchy module. We also import matplotlib to visualize the results.

Next, we load our customer data and select two columns: annual income and spending score. After that, the key line runs: linkage(X, method="ward"). This single function performs the entire merge process we described earlier and returns the complete merge history.

Finally, dendrogram(merged) draws that tree so you can visually inspect it. At this point, you’d look at the chart and decide where to make your cut.

Step 2: Cut the Tree into Real Cluster Labels

from sklearn.cluster import AgglomerativeClustering

# Already decided from the dendrogram: 3 clusters looks right
model = AgglomerativeClustering(n_clusters=3, linkage="ward")
labels = model.fit_predict(X)

data["cluster"] = labels
print(data.groupby("cluster").mean())

Once you’ve inspected the dendrogram and decided that three clusters looks right, this second step brings in AgglomerativeClustering from scikit-learn. We create the model with n_clusters=3 and linkage="ward", matching the settings we used to build the dendrogram.

Then, we call fit_predict(X), which returns a label for every single row in our dataset. Afterward, we attach these labels back onto our dataframe. Finally, we print the average values per cluster to understand what each group represents.

Notice something important here. There’s no elbow-method guesswork involved. We already knew the right number of clusters because we read it directly from the dendrogram cut in step one.

Hierarchical Clustering vs. K-Means

Both techniques group data, but they behave quite differently. Understanding these differences will help you choose the right tool for your next project.

FactorHierarchical ClusteringK-Means
Choosing cluster countDecide after, from the treeMust decide before running
Result stabilitySame result every runCan vary with random start
Best dataset sizeSmall to medium datasetsScales to very large datasets
What you get for freeA full tree of relationshipsJust the final flat groups

Neither technique is strictly better than the other. Instead, your choice depends on your dataset size and what you need from the output. If you want to explore relationships between clusters, Hierarchical Clustering wins easily. However, if your dataset contains millions of rows, K-Means will scale far more efficiently.

Common Mistakes to Avoid

Even experienced practitioners run into a few common pitfalls with Hierarchical Clustering. Let’s cover the three biggest ones so you can avoid them.

First, it gets slow fast. Comparing every pair of points is computationally expensive. Consequently, agglomerative clustering struggles once your dataset grows past a few thousand rows. If you’re working with large data, consider sampling first or switching to a more scalable method.

Second, outliers distort merges. A single stray point can force an early, misleading merge. Therefore, it’s worth cleaning outliers from your dataset before running the algorithm. Otherwise, your entire tree structure can shift because of one bad data point.

Third, linkage choice matters more than you’d expect. Single linkage, in particular, can chain unrelated points together into one long, stretched-out cluster. This effect is sometimes called “chaining.” As a result, Ward’s method or complete linkage usually produces more sensible, compact clusters for everyday datasets.

Real-World Applications

Hierarchical Clustering shows up across many industries. Here are a few practical examples that demonstrate its versatility.

In customer segmentation, businesses use it to group customers by purchasing behavior. Because the dendrogram reveals natural groupings, marketing teams can identify both broad segments and finer sub-segments within them, all from a single analysis. For example, a retailer might discover a broad “budget shoppers” segment that splits further into weekend bargain hunters and clearance-sale regulars, insights a flat clustering method could easily miss.

In biology and genetics, researchers use hierarchical clustering to build phylogenetic trees, which show evolutionary relationships between species. Since the technique naturally produces a tree structure, it fits this use case perfectly. Scientists can trace how closely related two species are simply by looking at how early or late their branches merge.

In document and text analysis, hierarchical clustering groups similar articles, support tickets, or research papers together. Teams can then explore topics at different levels of granularity, from broad categories down to closely related sub-topics. A support team, for instance, might start with a general “billing issues” cluster and drill down into more specific complaint types.

Finally, in anomaly detection, unusually small or oddly placed clusters near the bottom of a dendrogram often signal outliers worth investigating further. This makes hierarchical clustering a useful diagnostic tool, not just a grouping technique. Fraud analysts, for example, sometimes use this property to flag transactions that don’t merge naturally with any larger group.

Key Takeaways

Let’s bring everything together into a few memorable points.

Hierarchical Clustering merges the two closest groups, again and again, until only one remains. The dendrogram simply shows a picture of every merge, in the order it happened. You get to cut the tree wherever you like, and that decision determines your final number of clusters. Underneath all of this structure, the technique relies on nothing more than straight-line distance.

That’s genuinely the whole algorithm. It’s simple, visual, and it never forces you to guess K before you’ve even looked at your data. Once you’ve traced through a dendrogram by hand a couple of times, the entire process starts to feel like second nature. That’s the real goal here: not memorizing formulas, but building genuine intuition you can rely on.

What’s Next?

Hierarchical Clustering assumes that every point eventually belongs to one big family tree. But what happens when your clusters take on unusual shapes, like rings or rivers? What about datasets where some points are simply noise and shouldn’t belong to any cluster at all?

That’s exactly what we’ll tackle in Episode 73: DBSCAN, Density-Based Clustering. If you found this article helpful, watch the full video walkthrough on the Intelevo YouTube channel, and subscribe so you don’t miss the next episode.

Leave a Comment

Your email address will not be published. Required fields are marked *