Hierarchical linkage
Quick answer
Agglomerative hierarchical clustering starts with singleton groups and repeatedly merges the closest pair of groups. A linkage rule defines distance between groups; a dendrogram records the merge history and lets you inspect partitions at different cut heights. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Compute group linkage distances
a=[0,2]; b=[5,8]d=[abs(x-y) for x in a for y in b]print(min(d),max(d),sum(d)/len(d))Observed local output:
3 8 5.5The same observations give different distances under different linkage rules.
How it works
For two groups A and B, single linkage uses min d(a,b), complete linkage uses max d(a,b), and average linkage uses the average of all cross-group distances. Ward linkage chooses the merge that minimizes the increase in within-group squared error, proportional to |A||B|/(|A|+|B|) times squared centroid distance. Ward requires Euclidean geometry. Example: A={0,2}, B={5,8}; cross distances are 5,8,3,6, so single=3, complete=8, average=5.5. Begin with singleton points 0,2,7; first merge 0 and 2 at height 2; then the single-link distance to 7 is 5, whereas complete linkage gives 7. Single linkage can connect groups through a chain of intermediate points. Complete linkage discourages very wide groups, but outliers can dominate its maximum. The tree is nested: once merged, groups cannot be split by a later agglomerative step. Tied distances can lead to different valid merge orders. SciPy linkage returns rows describing the two merged groups, merge distance and total count; singleton IDs 0..n-1 are followed by newly assigned group IDs. A cut at a fixed height or group count summarizes the tree; it does not establish that the groups have real meaning. Pairwise distance storage can be quadratic in n; avoid scaling this toy approach blindly. Native AgglomerativeClustering in scikit-learn does not provide a general predict method for new observations.
Inspect merge heights
from scipy.cluster.hierarchy import linkagefor method in ["single","complete"]: z=linkage([[0],[2],[7]],method=method) print(method,z[:,2].tolist(),z[:,3].astype(int).tolist())Observed local output:
single [2.0, 5.0] [2, 3]complete [2.0, 7.0] [2, 3]The second merge height changes while the merge sizes do not.
Common mistakes
Do not feed a square distance matrix to scipy linkage as though it were a condensed distance vector. Do not use Ward with arbitrary non-Euclidean distances. A long branch is a clue about merge geometry, not automatically a confidence interval.
Practice and next steps
Compute the three cross-group linkage distances for A={1,4}, B={6,10}; answer: 2,9,5.5. Compare single and complete linkage on a chain of equally spaced points and explain why they can group differently. Complete the knowledge check and staged exercise. Next: Density clustering with DBSCAN.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Hierarchical linkage — knowledge check
Apply hierarchical linkage to new scenarios.
What is single linkage?
Which rule uses the farthest pair?
What geometry is required by Ward?
What does a dendrogram record?
Can ordinary AgglomerativeClustering predict unseen points directly?