Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #1405

Hierarchical Clustering

Hierarchical clustering builds a full tree of nested clusters — from every point in its own cluster up to one giant cluster containing everything — letting you choose how many clusters you want after seeing the whole structure, rather than committing to \(k\) upfront.

Agglomerative vs Divisive

ApproachDirectionHow Common
AgglomerativeBottom-up: start with every point as its own cluster, repeatedly merge the closest pairThe standard, widely-used approach — see Agglomerative Clustering
DivisiveTop-down: start with one cluster containing everything, repeatedly splitRare in practice — computationally expensive

The Dendrogram — Reading the Whole Tree at Once

distance A B merge at 1 C D merge at 1 merge at 5 (final) cut here → 2 clusters

Cutting the dendrogram at any height gives a different number of clusters — cutting below the final merge (at height 5) gives 2 clusters: {A,B} and {C,D}.

Worked Example

4 points: \(A(1,1)\), \(B(2,1)\), \(C(5,5)\), \(D(6,5)\).

PairDistance
A-B1.0
C-D1.0
B-C5.0
A-C≈ 5.66
B-D≈ 5.66
A-D≈ 6.40

Step 1: merge the closest pair, A-B (distance 1), into cluster {A,B}. Step 2: merge the next closest pair, C-D (distance 1), into cluster {C,D}. Step 3: merge {A,B} and {C,D} — using single linkage (the minimum distance between any pair across the two clusters), that's B-C at distance 5, so the final merge happens at height 5.

from scipy.cluster.hierarchy import dendrogram, linkage
import numpy as np

X = np.array([[1,1],[2,1],[5,5],[6,5]])
Z = linkage(X, method="single")   # single linkage, matching the hand calculation
print(Z)   # each row: [cluster1, cluster2, distance, num_points_in_new_cluster]

dendrogram(Z, labels=["A","B","C","D"])

Linkage Criteria — How "Distance Between Clusters" Is Defined

LinkageDefinition
SingleMinimum distance between any pair of points across the two clusters
CompleteMaximum distance between any pair — tends to produce more compact, evenly-sized clusters
AverageAverage distance across all pairs
WardMinimizes the increase in total within-cluster variance from merging — often the most balanced default

Practical Use Cases

  • When you want to explore multiple possible numbers of clusters without re-running the algorithm — one dendrogram supports cutting at any height
  • Gene expression analysis, document taxonomy building, and other domains where a natural hierarchy of groupings is meaningful

Common Mistakes

  • Choosing single linkage by default without realizing it can produce long, straggly "chained" clusters — complete or Ward linkage often gives more intuitive, compact groupings.
  • Running hierarchical clustering on very large datasets — it's \(O(n^2)\) or worse in both time and memory, impractical much beyond a few thousand points.

Interview Relevance

Q: "What's the main advantage of hierarchical clustering over K-Means?" You don't need to choose the number of clusters upfront — the dendrogram shows the full nested structure, and you can decide how many clusters to use by cutting at different heights, after seeing the whole picture.

Practice Question

Using the 4-point example, explain why complete linkage (using the maximum, not minimum, distance) would merge {A,B} and {C,D} at a different height than single linkage did.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →