How do you interpret a dendrogram to choose where to cut the tree?
Last updated
The vertical axis of a dendrogram is the merge distance (dissimilarity), so you cut horizontally where there is a large vertical gap between successive merges: that gap means you would have to join clusters that are far apart, so the clusters below it are well separated.
Reading the tree:
Leaves are observations; each horizontal bar is a merge drawn at the height (distance) where it happened.
Horizontal position of leaves is arbitrary. Similarity is read from merge height, not from how close two leaves sit on the x-axis.
A horizontal line at height h crosses k vertical branches, which gives k clusters.
Choosing the cut:
Largest-gap heuristic: find the longest vertical stretch with no merges and cut through it. This is the most stable k across a range of thresholds.
Elbow on merge heights: plot the sorted merge distances and look for a sudden jump. This is the hierarchical analogue of the k-means elbow.
Distance threshold: cut at a domain-meaningful distance, e.g.
fcluster(Z, t, criterion='distance')in SciPy ordistance_thresholdin scikit-learn.Validate the candidate k values with internal metrics (silhouette) or with business usefulness.
Caveats:
The heights depend on the linkage method. Ward produces exaggerated top-level gaps; single linkage produces compressed, chained trees.
A single flat cut assumes all clusters share a similar scale. Clusters of very different density may need non-uniform (dynamic) cuts.
[lang:python]
from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np
Z = linkage(X, method='ward')
gaps = np.diff(Z[:, 2]) # jumps between successive merge heights
i = gaps.argmax()
t = (Z[i, 2] + Z[i + 1, 2]) / 2 # cut in the middle of the largest gap
labels = fcluster(Z, t, criterion='distance')