Sub topic 6
Reading a dendrogram
This chapter assumes you know how agglomerative clustering merges the two closest clusters at each step, and that you have seen the linkage criteria (single, complete, average, Ward). You also need basic Python and matplotlib.
What the plot shows
A dendrogram is a drawing of the full merge history. Each leaf on the horizontal axis is one sample. Each U-shaped link joins two clusters. The height of the link's horizontal bar is the merge distance, which is the value the linkage criterion computed at that step.
The diagram below shows the structure for four points.
Drawing one
The code below builds a tree with average linkage and plots it.
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=30, centers=3, random_state=0)
Z = linkage(X, method="average")
dendrogram(Z)
plt.xlabel("sample index")
plt.ylabel("merge distance")
plt.show()The plot is a picture of the matrix Z. Each row of Z is one merge: two cluster indices, the merge distance, and the size of the new cluster. The last rows are the highest merges in the tree.
from scipy.cluster.hierarchy import linkage
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=30, centers=3, random_state=0)
Z = linkage(X, method="average")
# Columns: cluster a, cluster b, distance, size of the merged cluster.
print(Z[-3:])How to read it
- Find the two samples you care about and trace upward until their branches join. The height of that join is the distance at which they became one cluster.
- Compare heights, not positions. Two leaves drawn side by side can be joined only near the top, because the plot has to put the leaves in some order.
- Ignore left-right flips. Swapping the two children of any link gives an equally valid drawing.
- Look for long vertical stretches with no merges. A big gap between one merge height and the next means the clusters at that level were far apart before they were forced together. This is where a cut is most natural, and the next chapter uses that idea.
Common errors
- Reading horizontal distance as similarity. Only the join height carries meaning.
- Comparing dendrograms across linkages or across differently scaled data. Scale features first, since unscaled features distort the heights.
- Expecting equal-sized clusters. Single linkage often produces a staircase pattern, where one cluster absorbs points one at a time (chaining). Ward tends to give compact, balanced branches.
- Treating a tall final merge as proof of a real structure. With few samples or noisy data, a large gap can appear by chance.
Exercise
In a dendrogram, leaves 3 and 17 sit next to each other on the x-axis, but their branches join at the very top of the plot. Are they similar?
Show answer
No. Their closeness on the axis is only a drawing order. Their join height is the maximum, so they sit in different clusters until the final merge, which means they are far apart under the chosen linkage.