Lessons › Chapter 7: Unsupervised learning
Hierarchical clustering
Hierarchical clustering starts by treating every point as its own tiny group: six points, six groups.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
Groups inside groups
Marketing now wants big segments and smaller sub-segments within them.
Every point starts alone
Hierarchical clustering starts by treating every point as its own tiny group: six points, six groups.
Merge the closest pair
Find the two closest groups and join them with a bridge. The bridge’s height shows how far apart they were. The closest pairs merge first, with low bridges.
Keep merging groups
Now merge groups of groups. The two bottom pairs are closer to each other than to the top pair, so they join next, with a higher bridge.
A family tree of the data
Keep going until everything is one group. The bridges form a dendrogram, a family tree of the data. Slice it at any height to choose how many groups you want: this cut gives 3.
In real life: family trees and news stories
Biologists build family trees of species from how similar their DNA is. News apps group dozens of articles about the same event into one story. Both are hierarchical clustering: merge the most similar first, then keep going.
Quick check: What does hierarchical clustering merge at each step?
- The two closest groups (correct)
- Two random points
- The two largest groups
It always joins the nearest pair of groups, building the tree from the bottom up.
Pseudocode
start: every point is its own group
repeat until one group is left:
find the two closest groups
merge them
draw the merges as a tree (a dendrogram)
cut the tree at some height → that many groups
Key terms
- dendrogram
- A tree diagram that shows the order in which groups were merged.