From one partition to a hierarchy
A partition gives one grouping of the observations. Often we want to examine several resolutions: two broad groups, then smaller groups within each one. Hierarchical clustering records such nested decisions in a tree. Its leaves are the individual observations, and an internal node records a group formed from its children. Cutting the tree selects a partition without fitting a new model for every requested number of groups.
The central choice is how to measure separation between groups. The closest pair of observations, the furthest pair, and the average over all pairs answer different questions. None of these quantities is the same as the distance between two means. A hierarchical method becomes precise only after its point distance, group linkage, and tie rule have been specified.
We use a finite dataset , with . A cluster is a nonempty set of observation indices. Different observations retain different indices even when their feature vectors coincide. At each stage, the active clusters form a partition: they are disjoint and together contain every index.
Four linkage criteria
Definition
Single, complete and average linkage
For disjoint nonempty clusters and point distance , define
Single linkage asks whether the groups have a nearby connection. Complete linkage asks whether every cross-pair is close. Average linkage measures the mean separation over all cross-pairs, with every observation pair receiving the same weight.
These criteria can be applied to a supplied symmetric dissimilarity matrix. Their algorithmic definitions do not require Euclidean coordinates. Metric properties matter when making additional geometric conclusions. With ordinary Euclidean distance, a chain of short gaps can join a group whose endpoints are far apart under single linkage. Complete linkage resists this particular effect because a distant cross-pair makes its linkage large.
Ward linkage instead measures an increase in squared error. Here the feature vectors lie in , and the norm is Euclidean. Write
Proposition
The Ward merge cost
For disjoint nonempty ,
Ward's method chooses an active pair minimizing .
Proof
Separate within-group variation from mean displacement
Let , and . Expanding and summing over removes the cross term because . The corresponding identity holds for . Thus
Since and , the two remaining terms add to .
The size factor is essential. Two pairs with the same distance between their means can have different merge costs because their cluster sizes differ. For a Ward dendrogram we use itself as the height. A convention such as gives the same merge ordering but different height values; state the convention before interpreting a plotted height as an error.
Worked example
Four answers for the same two groups
Take and on the real line. The four cross-distances are . Therefore
For the final number, both sizes are two and the means are and . Also , and , so the increase is . The average pairwise distance is six, while the Ward cost is thirty-six: these criteria have different meanings and units.
Agglomeration and exact tree cuts
Agglomeration starts with singleton clusters. Compute the linkage of every active pair, merge a pair with the smallest value, and replace its two members by their union. Repeat until one cluster remains. Each merge reduces the number of active clusters by one, so there are exactly merges.
For reproducibility, break an exact tie using the lexicographic order of the sorted observation-index lists in the candidate pair. A different tie rule can produce another valid tree when minimum linkage values coincide. The tie rule is part of the procedure, rather than evidence of a numerical failure.
After merges, exactly clusters remain. This merge-count cut always exists for . A horizontal height cut is a different operation: it merges everything below a chosen threshold. Equal-height merges can make the number of resulting groups jump, so a height threshold need not realize every integer .
Worked example
Different linkages create different nested groups
Use the indexed points , in that order. Apply the stated tie rule. The complete merge records for two criteria are:
| Merge | Single linkage | Height | Complete linkage | Height |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 |
At the two-group cut, single linkage gives and ; complete linkage gives and . The difference follows directly from the objectives. The single-link chain can grow through gaps of two, whereas complete linkage must account for the distance from three to eight.
Updating linkage values
Recomputing every point pair after each merge hides useful structure. For a third active cluster , let and . Then
The average formula follows by splitting the double sum into pairs from and pairs from . Taking the unweighted average of two old linkage values would assign the same total weight to groups of different sizes. That defines a different method. The minimum and maximum formulas follow by taking the extremum over the union of the two cross-pair sets.
These updates also explain nondecreasing merge heights for these three criteria. When a pair is merged at the current minimum , every remaining pair value is at least . The new minimum, maximum or weighted average of old values is also at least . Repeating the argument proves that the height sequence cannot decrease.
Proposition
Single-link cuts are graph components
Fix a threshold . Construct the graph whose vertices are observation indices, with an edge whenever . The single-link hierarchy cut after all merges of height at most gives exactly the connected components of this graph.
Proof
A crossing edge is exactly an available merge
If an edge of length at most joins two active groups, their single linkage is at most , so the algorithm cannot finish the threshold cut while leaving those groups separate. All vertices on a graph path therefore end in the same group. Conversely, every merge of height at most joins groups containing a cross-edge of length at most . Starting from singletons, such a merge stays inside a graph component. Both inclusions follow.
A component can have a large diameter despite containing only short edges along a connecting path. The graph description makes the chaining effect precise. It also explains why deleting one bridge may split a large single-link group while hardly changing most pairwise distances.
A small executable merge record
This direct implementation uses complete linkage. It keeps indices separate from coordinates, and uses tuple ordering for exact ties. It computes the criterion from its definition, which is convenient for checking a short hand calculation.
import numpy as np
x = np.array([0., 3., 4., 6., 8.])
clusters = [(i,) for i in range(len(x))]
while len(clusters) > 1:
candidates = []
for i in range(len(clusters)):
for j in range(i + 1, len(clusters)):
a, b = clusters[i], clusters[j]
height = np.abs(x[list(a), None] - x[list(b)]).max()
candidates.append((float(height), a, b, i, j))
height, a, b, i, j = min(candidates)
print(a, b, height)
clusters = [c for k, c in enumerate(clusters) if k not in (i, j)]
clusters.append(tuple(sorted(a + b)))
clusters.sort()
The printed heights are . The tuples are zero-based observation indices: the first merge refers to the coordinates three and four. A linkage record should preserve this distinction so that duplicate feature vectors do not lose their observation identities.
Divisive hierarchies
A top-down hierarchy starts with the full dataset and splits one active cluster at a time. For a concrete procedure, choose an active cluster with the largest number of observations, break size ties by its index list, and split that cluster by two-means. Specify the two-means initialization and tie handling as well. Once a split is made, later operations occur inside its children. They do not transfer observations across the earlier boundary.
Worked example
Splitting the largest group
Start with . A two-means split initialized at zero and twenty stabilizes at the groups and , with means and . The first group is larger. Splitting it with initial means zero and nine gives and . The resulting three groups have total within-group squared error , compared with just before the second split. The later split refines its parent; it does not undo or reconsider the isolated group containing twenty.
Divisive and agglomerative methods make different irreversible decisions. A sequence of two-means splits is consequently not the reverse of a Ward merge sequence. Both produce nested partitions, but their search procedures and local decisions differ. A tree records those decisions; it does not establish that every cut minimizes a global clustering objective.
Interpreting merge heights
A height has meaning only through its linkage. For complete linkage, a group formed by merges of height at most a threshold has diameter at most that threshold. This follows by induction: singleton diameters are zero; when two groups merge, every cross-distance is at most their complete linkage, while distances inside each child already satisfy the bound. Thus the threshold controls every pair in the resulting group, not just its last connection.
For single linkage, the threshold instead controls the largest edge needed along a connecting path. The endpoints of a long chain can be much farther apart. For average linkage, the height is an average over the current cross-pairs. The earlier groups at zero and two versus five and nine have average linkage six, although the merged diameter is nine. A statement about an average does not turn into a uniform bound on every member pair.
Ward heights accumulate differently. At each merge the total within-group squared error increases by exactly its recorded cost. Starting from singletons, whose error is zero, adding all the Ward costs up to a particular merge-count cut gives the total squared error of that partition. This supplies a direct numerical check on a merge record. The relation uses the raw squared-error heights defined here; adding their square roots would not recover that total.
Choosing a cut therefore requires a substantive question. A complete-link threshold can express a maximum within-group dissimilarity. A Ward cut can be compared using its total reconstruction error. A large jump in successive heights suggests that the next merge joins more separated groups under the chosen criterion. The interpretation remains conditional on the feature geometry and linkage rather than being a universal test for a unique number of natural groups.
Feature geometry in image grouping
For an image example, regard each pixel as an indexed observation. Let be its grayscale intensity and its row and column. One possible feature vector is , where fixes the relative spatial scale. Then
With zero spatial weight, pixels with similar intensities can belong to the same group even if they are far apart in the image. A positive weight adds a cost for spatial separation. Its magnitude must be interpreted relative to the intensity units: multiplying every intensity by a constant changes that balance unless the spatial weight is adjusted as well. Ward linkage then measures increased squared error in this chosen feature space.
A spatially connected segmentation requires explicit adjacency constraints on admissible merges. Including pixel coordinates as features alone does not impose such a constraint. This illustrates a general modeling distinction: the feature map defines similarity, whereas allowed merges define which partitions the algorithm is permitted to construct. Both choices affect the resulting tree and should be held fixed when comparing cuts.
Exercises
Checkpoint
1. Updating an average linkage
Suppose , , and . Find the linkage from to .
Solution · Exercise 1 solution
The weights are and , so the answer is . The unweighted average gives too much total influence to the smaller group .
Checkpoint
2. Ward height versus Euclidean distance
For the singleton groups at zero and four, calculate their Euclidean distance and their Ward merge cost under the height convention used here.
Solution · Exercise 2 solution
The distance is four, whereas . The merged mean is two and its squared error is .
Checkpoint
3. A cut with tied merge heights
The points use single linkage. Explain why a merge-count cut can give two groups even though a horizontal height threshold cannot.
Solution · Exercise 3 solution
All three merges occur at height one. Two completed merges leave exactly two groups, with their membership depending on the stated tie order. A threshold below one leaves four singletons; a threshold at least one joins the whole connected chain. Neither threshold gives two groups.
Checkpoint
4. What a divisive refinement can change
After a divisive split produces and , a later split divides into . Can an observation in move into during that step?
Solution · Exercise 4 solution
No. A hierarchical refinement requires and disjoint children. Moving an observation from would violate that nesting. Reassigning all observations by new centers would define a different, non-hierarchical update.
Related notes
- Center objectives and Lloyd updates derives the squared-error updates used in a two-means split.
- Density connectivity and mean shift distinguishes core-point connectivity from unrestricted single-link chains.
- Course reading list collects the recommended books.