Part IV — Clustering · Final Chapter

Chapter 20 · Hierarchical Clustering

K-Means demanded k before it would start. Hierarchical clustering refuses the question: merge the two closest points, then the two closest clusters, and keep going until everything is one — producing a dendrogram that contains every k simultaneously. This chapter builds the distance matrix and all four linkage rules by hand, reads the real dendrogram's merge-height cliff, watches single linkage fail catastrophically on real data — and finds the same five mall segments K-Means found, the strongest structural evidence yet.

📖 Chapter 20 of 33 ⏱ ~85 min read Mostly Core Stable 📝 Quiz · 30 MCQs

🎯 Learning Objectives

1. Every k at Once

Chapter 19's awkward opening question — "how many clusters?" — had to be answered before the algorithm ran, and every instrument for answering it (elbow, silhouette) required fitting the model many times. Hierarchical clustering inverts the deal: run once, and receive a complete map of every possible grouping.

The practical family is agglomerative (bottom-up): every customer starts as their own cluster; repeatedly find the two closest clusters and merge them; stop when everything is one cluster. The theoretical twin — divisive (top-down: start with one cluster, split recursively) — is rarely implemented, because deciding the best split is far harder than deciding the best merge. Same mall data as Chapter 19: 200 customers, Income × Spending, and the same question — how many segments, and who is in each?

📝
Note: the algorithm's output is a tree, not a partition. The partition appears only when you cut the tree — which is why hierarchical clustering never asks you for k in advance: it asks you after, with all the evidence on the table.

2. The Distance Matrix & Linkage, by Hand

Two ingredients replace K-Means' centroids. First, the pairwise distance matrix — the distance between every two customers. Second, a linkage rule — how to measure the distance between clusters once they contain more than one point. Five customers on a line, spending scores 1, 2, 6, 8, 9; the full matrix, every entry by hand (|a − b|):

12689
101578
210467
654023
876201
987310

Now merge, step by step — and here is where linkage enters:

Same data, same final clusters — but the merge heights differ by linkage, and heights are the dendrogram's entire shape. The four rules, in full:

LinkageCluster distance =PersonalityWatch out
singlemin pairwise distanceFriendly — merges on one close pairChaining: snakes across gaps (Section 4's real failure)
completemax pairwise distanceStrict — whole clusters must be closeOutliers stretch every distance
averagemean pairwise distanceThe compromiseCan produce awkward in-between shapes
wardthe merge that least increases total WCSSCompact, balanced clustersAssumes (roughly) spherical clusters, like K-Means
💡
Pro Tip: ward is "K-Means' cousin" — both minimise squared distances to cluster centres (ward merges by the same WCSS Chapter 19 defined; K-Means refines centroids). That kinship explains this chapter's ending: the two methods finding the same five segments.

3. The Dendrogram

Every merge is drawn as a upside-down U: two branches joining at the merge height. The result — the dendrogram — is the algorithm's complete autobiography. On our real data (ward linkage), the final eight merge heights, top-down:

406 · 395 · 263 · 246  ⇦⇦ the cliff ⇒⇦  114 · 113 · 107 · 81
The tree's last four merges (heights 246–406) stitch five big segments into super-groups; before them, everything merged at heights ≤ 114. That gap — 114 to 246 — is the data announcing its natural k: cutting the dendrogram at any height in the gap (say 200) severs exactly those last merges and leaves 5 clusters.
cut at 200 Target Careless Careful Sensible Std final merge 406 246 263 114 113 107 merge height → the four tall merges are the five segments' union — cutting in the 114–246 gap leaves 5 clusters
Figure 20.1 — The dendrogram's decision anatomy (real ward merge heights, branches simplified): the cut line's height chooses k.

Notice what the dendrogram adds over elbow and silhouette: it shows which clusters merge at which height — structure at every scale, visible before any k is chosen. Move the cut line down to 100 and you get 6+ clusters; up to 300 and the segments begin fusing into super-groups. No refitting — the tree already contains every answer.

4. Linkage in Practice: a Real Failure

Section 2's little line of points was too polite to expose the linkages' differences. The real mall data is not polite. All four rules, k = 5, same distance metric (real results):

LinkageSilhouetteCluster sizesVerdict
ward0.553032, 85, 39, 21, 23Five balanced segments
complete0.553039, 85, 32, 21, 23The same partition as ward
average0.479238, 102, 36, 21, 3Degrading — a 3-customer fragment peels off
single0.26953, 193, 1, 1, 2Catastrophe — chaining

Single linkage's 193-customer blob plus four orphans is chaining in its purest form: the rule only needs one close pair to merge, so the cluster snakes from customer to customer across the whole scatter — every intermediate point dragging in its neighbour. Average linkage starts down the same road (that orphan cluster of 3 is the beginning of fragmentation). Ward and complete, both demanding that clusters be coherent as wholes, land on the same balanced five.

⚠
Warning — "default" is a hypothesis too: the source's linkage='ward' is the right default for blobby data, but the table above is why you check: swap in single linkage on elongated data and it may be the right tool (it traces snakes well) while ward carves them wrongly. Linkage choice encodes a belief about cluster shape — state it, test it.
📋 Stable content — Reviewed: August 2026

5. Implementation in Python

import numpy as np
import pandas as pd

dataset = pd.read_csv('Mall_Customers.csv')
X = dataset.iloc[:, [3, 4]].values       # Income, Spending

# 1. The dendrogram (scipy) — the map of every k
import scipy.cluster.hierarchy as sch
dendrogram = sch.dendrogram(sch.linkage(X, method='ward'))
plt.title('Dendrogram'); plt.xlabel('Customers')
plt.ylabel('Euclidean distances'); plt.show()
# read the tallest unbroken spans -> cut -> count clusters

# 2. The cut, as a fitted model (sklearn)
from sklearn.cluster import AgglomerativeClustering
hc = AgglomerativeClustering(n_clusters=5, metric='euclidean',
                             linkage='ward')
y_hc = hc.fit_predict(X)
print(np.bincount(y_hc))                 # [32 85 39 21 23]

# 3. The linkage A/B test of Section 4
from sklearn.metrics import silhouette_score
for link in ['ward', 'complete', 'average', 'single']:
    m = AgglomerativeClustering(n_clusters=5, metric='euclidean',
                                linkage=link).fit(X)
    print(link, silhouette_score(X, m.labels_).round(4),
          np.bincount(m.labels_))

Listing 20.1 — Dendrogram, cut, and the linkage comparison (source: hierarchical_clustering.py, modernised).

💡
Pro Tip — API rename: the source's affinity='euclidean' was renamed metric='euclidean' in sklearn ≥ 1.2 (affinity is deprecated). Old notebooks will show the old spelling — both mean the distance used between points.

6. The Segments vs K-Means

The climax the chapter has been building toward. Ward hierarchical, k = 5, centres and sizes — side by side with Chapter 19's K-Means segments (all real):

SegmentK-Means (Ch 19)Hierarchical ward (Ch 20)Reading
Target(87, 82) · 39(87, 82) · 39Identical
Standard(55, 50) · 81(56, 49) · 854 boundary customers reassigned
Careful(88, 17) · 35(89, 16) · 323 customers differ
Careless(26, 79) · 22(25, 80) · 211 customer differs
Sensible(26, 21) · 23(26, 21) · 23Identical

Two algorithms with different machinery — centroid refinement versus merge trees — converge on the same five segments, centres within a point and sizes within a handful of boundary customers. And the ward silhouette sweep peaks at k = 5 (0.5530) exactly where Chapter 19's elbow and silhouette did. Three instruments, two algorithms, one structure. When independent methods agree this closely, the segmentation stops being an algorithmic output and starts being a fact about the customers — the strongest evidence clustering can offer, because in Part IV there is no ground truth to check against.

7. K-Means vs Hierarchical — Settled

K-Means (Ch 19)Hierarchical (Ch 20)
kMust be fixed before runningChosen after, by cutting the dendrogram — every k from one run
OutputCentroids + assignments (a deployable model)Assignments only — no centroid object for new points
New pointsAssign instantly to nearest centroidNo native answer — refit or improvise (nearest cluster member)
DeterminismInit lottery (needs k-means++ and restarts)Deterministic — same data, same tree
CostFast: roughly O(n·k·iterations)O(n²) distance matrix — memory and time explode on big n
Shape biasSpherical (ward-like)Depends on linkage — single even traces snakes
Best habitatProduction, large data, repeated assignmentExploration, small-to-medium n, unknown structure at many scales
🌎
Real World — the workflow, not the war: professionals use them in sequence, not in competition. Hierarchical first (200–2,000 rows): read the dendrogram, discover the natural k, see structure at every scale. Then K-Means at that k for deployment: centroids assign the next million customers cheaply. Exploration and production, each doing what it was built for.

8. Strengths, Limits & the Road Ahead

StrengthsLimitations
No k in advance — the dendrogram holds every answer O(n²) — thousands of rows, not millions
Deterministic; rich visual story for stakeholders No model object — new points don't assign natively
Linkage choice adapts to believed cluster shape Linkage choice is a belief — single linkage fails badly (real: 0.27)
Agreement with K-Means = structural evidence Early merges are greedy and unfixable — no lookahead

Part IV is complete: two structure-finders, judged without labels and cross-validated by each other's agreement. Part V changes the question one more time — from who groups together to what occurs together. Association rule learning abandons distance entirely: baskets, not points, and patterns like "people who buy X usually buy Y" — starting with the Apriori algorithm and the three numbers that govern it: support, confidence and lift.

🛠 Hands-On: Cut the TreeIntermediate

The segmentation team wants dendrogram-based evidence for k = 5 — not because K-Means said so, but because the tree does. Work the hand example, read the real merge heights, run the linkage gauntlet, and deliver the cross-algorithm verdict.

Steps

  1. By hand: build the 5×5 distance matrix for {1, 2, 6, 8, 9}; trace merges under single and complete; confirm the final heights 4 vs 8.
  2. Fit the ward dendrogram on the mall data; extract the last 8 merge heights; locate the gap that argues for k = 5.
  3. Cut into 5 clusters; extract centres and sizes; match them to Chapter 19's segments.
  4. Run all four linkages at k = 5; tabulate silhouettes and sizes; name single linkage's failure mode from the numbers.
  5. Run ward's silhouette sweep k = 2..10; confirm the peak.
  6. Write the cross-algorithm verdict paragraph: why agreement strengthens the segmentation, and which method ships for exploration vs production.
View Solution / Walkthrough
import numpy as np, pandas as pd
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import silhouette_score
from scipy.cluster.hierarchy import linkage

X = pd.read_csv('Mall_Customers.csv').iloc[:, [3, 4]].values

Z = linkage(X, method='ward')
h = np.sort(Z[:, 2])[::-1]
print('top merge heights:', h[:8].round(0))     # the cliff

for link in ['ward', 'complete', 'average', 'single']:
    m = AgglomerativeClustering(n_clusters=5, metric='euclidean',
                                linkage=link).fit(X)
    print('%-9s sil=%.4f sizes=%s' % (link, silhouette_score(X, m.labels_),
                                      np.bincount(m.labels_)))

for k in range(2, 11):
    m = AgglomerativeClustering(n_clusters=k, metric='euclidean',
                                linkage='ward').fit(X)
    print('k=%2d sil=%.4f' % (k, silhouette_score(X, m.labels_)))

hc5 = AgglomerativeClustering(n_clusters=5, metric='euclidean',
                              linkage='ward').fit(X)
for c in range(5):
    m = X[hc5.labels_ == c]
    print(c, '(%.0f, %.0f)' % (m[:,0].mean(), m[:,1].mean()), len(m))

Expected results (real):

  • Step 1: matrix as in Section 2; merges {1,2} & {8,9} at 1; {6}+{8,9} at 2 (single) / 3 (complete); final merge at 4 (single) vs 8 (complete).
  • Step 2: heights 406, 395, 263, 246 then 114, 113, 107, 81 — the 114–246 gap; cut at ~200 → 5.
  • Step 3: (87,82)·39 Target, (56,49)·85 Standard, (89,16)·32 Careful, (25,80)·21 Careless, (26,21)·23 Sensible — matching Ch 19 within boundary customers.
  • Step 4: 0.5530 / 0.5530 / 0.4792 / 0.2695; single's [3, 193, 1, 1, 2] = chaining into one giant blob.
  • Step 5: ward sweep peaks at k = 5 (0.5530) — elbow, K-Means silhouette and the tree all agree.
  • Step 6: two algorithms, one structure — evidence the five segments are real; hierarchical for exploration, K-Means for production assignment.

🎯 Key Takeaways

1

Hierarchical clustering builds every k at once: merge nearest clusters repeatedly, cut the dendrogram where the data says cut.

2

Linkage defines cluster distance — single (min) chains, complete (max) is strict, average compromises, ward minimises WCSS growth like K-Means' cousin.

3

The real merge-height cliff (114 → 246) IS the k evidence: cutting inside the gap leaves exactly the five segments.

4

Single linkage failed for real: silhouette 0.27 with a 193-customer blob — chaining. Ward and complete (0.5530) produced the balanced five.

5

Ward's five segments match K-Means' five within boundary customers — two independent algorithms agreeing is the strongest label-free evidence.

6

The professional sequence: hierarchical to explore and choose k, K-Means to deploy — dendrogram for insight, centroids for the next million customers.

Test Your Understanding

30 questions across the chapter — the algorithm, linkage by hand, the dendrogram, the real linkage failure, and the K-Means comparison. Choose one answer per question, then check your answers.

Q1.Agglomerative clustering works:

Q2.Hierarchical clustering's headline advantage over K-Means:

Q3.(Hand) In the distance matrix for points {1, 2, 6, 8, 9}, the closest pair is:

Q4.Single linkage measures the distance between two clusters as:

Q5.Complete linkage measures it as:

Q6.Ward linkage chooses each merge by:

Q7.(Hand) Merging {1,2} with {6,8,9}: single linkage's height vs complete linkage's:

Q8.The y-axis of a dendrogram records:

Q9.(Real) On the mall data, cutting the ward dendrogram at height ≈ 200 yields:

Q10.(Real) The last eight merge heights read 406, 395, 263, 246, then 114, 113, 107, 81. The k-evidence is:

Q11.(Real) Single linkage, k = 5, produced silhouette 0.2695 with sizes [3, 193, 1, 1, 2]. The diagnosis:

Q12."Chaining" is the classic failure of:

Q13.(Real) Ward and complete linkage both scored 0.5530 with the same partition. Average scored 0.4792. The ranking's lesson:

Q14.(Real) Ward hierarchical's five segments versus Chapter 19's K-Means five:

Q15.Two clustering algorithms with different machinery agreeing on the same structure is:

Q16.(Real) Ward's silhouette sweep peaked at k = 5 (0.5530) — the same k as Chapter 19's elbow and K-Means silhouette. The tally:

Q17.Unlike K-Means, hierarchical clustering is:

Q18.Hierarchical clustering's scaling wall:

Q19.Divisive clustering — the top-down twin:

Q20.The source code's affinity='euclidean' argument:

Q21.The dendrogram's unique product:

Q22.Average linkage defines cluster distance as:

Q23.Unlike K-Means, a fitted hierarchical clustering offers:

Q24.The natural habitat of hierarchical clustering:

Q25.The production role where K-Means wins:

Q26.Calling ward "K-Means' cousin" refers to their shared:

Q27.Hierarchical clustering and feature scaling:

Q28.(Real) Average linkage's sizes [38, 102, 36, 21, 3] versus ward's [32, 85, 39, 21, 23] show:

Q29.Part V's subject abandons distance for co-occurrence:

Q30.The professional two-step this chapter endorses: