Skip to content

Hierarchical Clustering (Dendrograms)

What hierarchical clustering does

Hierarchical clustering creates a hierarchy (tree) of clusters. Unlike K-Means, you never have to decide kk up front — you build the whole tree and decide where to “cut” it afterward.

Two main styles:

  • Agglomerative (bottom-up): start with points, merge them
  • Divisive (top-down): start with one cluster, split it

Think of many tiny soap bubbles floating on water and gradually sticking together until there’s one big cluster of bubbles. At each step, agglomerative clustering connects the two nearest clusters (starting from individual instances). Draw a branch for every merge and you get exactly the binary tree structure shown below — a dendrogram.

Dendrogram intuition

A dendrogram is a tree diagram showing merge/split steps.

diagram Diagram mermaid

Linkage criteria

How we measure distance between clusters:

  • single linkage (min distance)
  • complete linkage (max distance)
  • average linkage
  • Ward linkage (common default in sklearn; minimizes variance)

Scikit-learn example

AgglomerativeClustering
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
 
X, _ = make_blobs(n_samples=60, centers=3, cluster_std=0.6, random_state=1)
 
hc = AgglomerativeClustering(n_clusters=3, linkage="ward")
labels = hc.fit_predict(X)
 
print("Cluster sizes:", np.bincount(labels))
AgglomerativeClustering
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
 
X, _ = make_blobs(n_samples=60, centers=3, cluster_std=0.6, random_state=1)
 
hc = AgglomerativeClustering(n_clusters=3, linkage="ward")
labels = hc.fit_predict(X)
 
print("Cluster sizes:", np.bincount(labels))

Pros and cons

Pros:

  • no need to choose K in advance (you can cut the dendrogram later)
  • can work with different distance metrics

Cons:

  • can be slow on large datasets

Visualize it

Watch a dendrogram grow from the bottom up: at each step, the two closest clusters merge (drawn as a “goalpost” — two vertical lines joined by a horizontal bar), and the process repeats until only one cluster is left. Click to shuffle the points:

sketch Agglomerative clustering builds a dendrogram p5.js
Each step merges the two closest clusters, drawing the tree from the leaves up until only one cluster remains.

Cutting the tree without choosing k up front

Because agglomerative clustering builds the whole hierarchy, you can decide how many clusters you want after the fact — either by asking for n_clustersn_clusters directly, or by cutting the dendrogram at a given merge distance with distance_thresholddistance_threshold:

cut_by_distance.py
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
 
X, _ = make_blobs(n_samples=60, centers=3, cluster_std=0.6, random_state=1)
 
# Instead of fixing k, cut the tree wherever merges start happening far apart
hc = AgglomerativeClustering(n_clusters=None, distance_threshold=10, linkage="ward")
labels = hc.fit_predict(X)
print("Clusters found by distance cut:", len(set(labels)))
cut_by_distance.py
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
 
X, _ = make_blobs(n_samples=60, centers=3, cluster_std=0.6, random_state=1)
 
# Instead of fixing k, cut the tree wherever merges start happening far apart
hc = AgglomerativeClustering(n_clusters=None, distance_threshold=10, linkage="ward")
labels = hc.fit_predict(X)
print("Clusters found by distance cut:", len(set(labels)))
Output
Clusters found by distance cut: 3
Output
Clusters found by distance cut: 3

Every fitted model also exposes the raw merge tree: n_leaves_n_leaves_ (one leaf per original instance) and children_children_ (one row per merge). For nn instances there are always exactly n - 1n - 1 merges — that’s the full dendrogram, whether or not you ask scikit-learn to cut it into clusters for you.

Mini-checkpoint

Try Ward linkage and complete linkage and see how clusters differ.

🧪 Try It Yourself

Exercise 1 – Fit Agglomerative Clustering

Exercise 2 – Cut the Dendrogram by Distance

Exercise 3 – Every Merge Builds the Tree

Next

Continue to DBSCAN: Density-Based Clustering — instead of merging everything into one tree, DBSCAN groups by density and is happy to leave some points out as noise.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did