Lecture 12 · room 202
HEMA · Università Cattolica del Sacro Cuore, Rome
Monday 9 November 2026
Lecture 11 · k-means · Tue 3 Nov
| Time | Min | |
|---|---|---|
| 17:00 | Last time · questions | 10’ |
| 17:10 | Growing the tree: linkage, chaining, by hand | 50’ |
| 18:00 | Break | 10’ |
| 18:10 | Reading a dendrogram · choosing and using | 40’ |
| 18:50 | Set 11 | 10’ |
Sometimes the number of groups is the question, not the input
Hierarchical clustering gives you all the partitions at once, from n groups down to one, and lets you choose after looking.
n - 1 merges. Record the distance at each. That record is the dendrogram.
Note
Step 2 hides the whole subject. “Closest” between two points is obvious. “Closest” between two clusters of points is a choice — the linkage.
The five patients of the next slide, merged one pair at a time. Each merge is drawn at the distance that produced it — and the same tree, cut twice, answers two different questions.
| Linkage | Distance between clusters A, B | Produces |
|---|---|---|
| Complete | farthest pair: \max_{a \in A, b \in B} d(a,b) | compact, similar-sized groups |
| Single | nearest pair: \min d(a,b) | long chains; finds odd shapes, and noise |
| Average | mean of all pairs | a compromise, often sensible |
| Ward | merge that increases within-SS least | k-means-like blobs; the usual default |
Same data, four linkages, four different trees. Ward and complete are the two you will use; single is the one you should know exists so you recognise its chaining when you see it.
Two clear groups and seven noisy points between them. Single linkage walks across the bridge one nearest-pair at a time and returns one cluster. Nothing in the output says this happened.
Length of stay, in days. Complete linkage.
\{2\},\ \{3\},\ \{6\},\ \{10\},\ \{11\}
Cut the tree at height 5 → two groups: short stays \{2,3,6\} and long stays \{10,11\}. Cut at 2 → three groups. Same tree, different question.
Attention
The most common misreading is the third bullet. Students — and consultants — see two leaves next to each other and call them similar. Look at the height they join at, nothing else.
Two leaves drawn side by side, joining only at the top of the tree. Then the same tree with a subtree flipped: the neighbours change, the tree does not. Any dendrogram can be redrawn this way, which is why horizontal position can carry no information.
| k-means | Hierarchical | |
|---|---|---|
| k | needed in advance | chosen after seeing the tree |
| Result | one partition | every partition, nested |
| Randomness | yes — nstart |
none |
| Scale | millions of rows | n^2 distances: a few thousand rows, then it stops |
| Shape | round blobs | depends on linkage |
Rule of practice: hierarchical to explore and decide k; k-means to deploy at scale with that k. They are colleagues, not rivals.
library(tidyverse); library(factoextra)
X <- scale(USArrests)
d <- dist(X) # Euclidean, on scaled data
hc <- hclust(d, method = "ward.D2") # or "complete", "average"
fviz_dend(hc, k = 4, rect = TRUE) # the tree, cut at 4
groups <- cutree(hc, k = 4) # group label per row
table(groups)Note
ward.D2 is Ward’s method on Euclidean distance as Ward defined it. Plain ward.D squares nothing and is there for backward compatibility. Use ward.D2.
The application where this method earns its keep
Attention
Cost dominates everything if you do not scale it. And a cluster with three patients is not a case-mix group; it is three outliers. Check group sizes before you name them.
And the cut is a decision you defend with the height it was made at.
USArrests with ward.D2, complete and single. Three trees. Which one chains?cutree() with Tuesday’s kmeans(k = 4) — table(km$cluster, groups). How many states move?Hierarchical clustering · about 40 minutes · before Fri 13 Nov
Not graded. The test is a mirror, not an exam.
?hclust, ?cutree, factoextra::fviz_dendStatistics & Big Data · HEMA, Università Cattolica del Sacro Cuore