Hierarchical Clustering
Learn how a computer builds a family tree of groups. It starts with separate data points and joins the closest groups step by step, creating a tree-shaped diagram called a dendrogram.
The big idea
Build a tree of groups instead of choosing all groups at the start.
Hierarchical Clustering is an unsupervised learning method. It joins similar data points into larger and larger groups. The joining process creates levels, like branches in a family tree.
You can later choose where to cut the tree to decide how many clusters you want.
Quick facts
Groups are arranged in levels, from small to large.
A tree diagram that shows when groups join.
A rule for measuring the distance between groups.
The algorithm finds groups without given answers.
1. How does it work?
We will focus on the bottom-up method, also called agglomerative clustering.
From separate points to one family tree
If there are 6 data points, begin with 6 tiny clusters.
Use a distance rule to decide which groups are nearest.
They become one bigger cluster.
Keep merging groups until all points are connected.
Choose a height to get the number of groups you want.
Two main directions
Agglomerative (bottom-up): starts with separate points and joins them into larger groups.
Divisive (top-down): starts with one large group and splits it into smaller groups.
2. Live Hierarchical Clustering Playground
Step through the merges and watch the dendrogram grow. Change the linkage rule to see how the tree can change.
Cluster map and dendrogram
Coloured points show the current groups. The tree shows the merge history.
Dendrogram
3. The mathematics behind Hierarchical Clustering
Distance tells the algorithm which groups should join.
Step 1: Distance between two points
This is the straight-line distance between two points.
Step 2: Distance between groups
A group can contain many points, so we need a rule to compare two groups.
Single linkage
between any point in A and B
Looks for the closest pair. It can create long, chain-shaped clusters.
Complete and average linkage
Average = mean of all pair distances
Complete favours tighter groups. Average uses all pair distances to balance the two.
4. Worked example: grouping eight learners
Imagine each learner is described by study hours and practice quizzes completed.
Learners with similar study habits may be grouped together. At the start, every learner is their own cluster. The algorithm repeatedly joins the closest groups.
| Learner | Study hours | Practice quizzes | Possible interpretation |
|---|---|---|---|
| A | 1.0 | 2 | Low study, low practice |
| B | 1.5 | 3 | Low study, low practice |
| C | 2.0 | 2 | Low study, low practice |
| D | 5.0 | 5 | Middle range |
| E | 5.5 | 6 | Middle range |
| F | 8.0 | 8 | High study, high practice |
| G | 8.5 | 7 | High study, high practice |
| H | 9.0 | 9 | High study, high practice |
5. How do we choose the number of clusters?
Read the dendrogram and look for large jumps in merge distance.
Cutting the dendrogram
Imagine drawing a horizontal line across the tree. Every branch the line crosses becomes a separate cluster. A higher cut usually gives fewer, larger clusters.
Look for large distance jumps
If two groups only join at a much larger distance than earlier merges, they may be quite different. A cut just below that jump can be useful.
6. Python implementation
Scikit-learn can build hierarchical clusters with AgglomerativeClustering.
Example code
Group learners using study hours and practice quizzes.
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
# Example data: [study hours, practice quizzes]
X = np.array([
[1.0, 2], [1.5, 3], [2.0, 2],
[5.0, 5], [5.5, 6],
[8.0, 8], [8.5, 7], [9.0, 9]
])
# Scale features because distance is important
X_scaled = StandardScaler().fit_transform(X)
# Create three clusters
model = AgglomerativeClustering(
n_clusters=3,
linkage="average"
)
labels = model.fit_predict(X_scaled)
print("Cluster for each learner:", labels)
# Draw a dendrogram to inspect the merge structure
tree = linkage(X_scaled, method="average")
dendrogram(tree)
plt.xlabel("Learner")
plt.ylabel("Distance")
plt.title("Hierarchical Clustering Dendrogram")
plt.show()7. Real-life applications
Hierarchical Clustering is useful when we want to explore relationships between groups.
Biology and genetics
Group genes or samples that have similar patterns.
Customer research
Explore groups of customers with similar interests or shopping habits.
Document grouping
Organise articles or reports by similarity after converting text into numbers.
Social network analysis
Explore communities or people with similar connection patterns.
Strengths
- Shows a hierarchy of groups.
- You can inspect different cluster counts.
- Does not need the number of clusters fixed before building the tree.
Limitations
- Can be slow on very large datasets.
- Results depend on the distance and linkage choices.
- Early merges are usually not undone.
- Scaling and outliers can affect distances.
8. Knowledge check
Answer all 10 questions, then submit to see your score and explanations.
Mini challenge
In the playground, switch between single, complete, and average linkage. Keep the data the same. Do the merge steps change?