Machine Learning · Chapter 18 of 40

Hierarchical Clustering

Builds a tree (dendrogram) by successively merging the closest clusters.

No need to specify K upfront — cut the dendrogram at any level to get any number of clusters.

Example 1 (python)
from sklearn.cluster import AgglomerativeClustering
h = AgglomerativeClustering(n_clusters=3).fit(X)
print(h.labels_[:10])
Output
[0 1 0 2 1 0 2 1 0 2]

Cluster labels.

Example 2 (python)
# Plot dendrogram with scipy.cluster.hierarchy.dendrogram

Visualise the merge tree.

Key points

  • Builds a merge tree.
  • No K needed to build.
  • Slower than K-means.
  • Dendrogram is very informative.
💡 Note: Agglomerative (bottom-up) is common; divisive (top-down) is rare in practice.

📝 Quick Quiz

1. Hierarchical clustering builds a:

2. It requires K upfront?

3. Common in practice: