ML0172 Hierarchical Clustering

Explain agglomerative clustering: linkage choices (single/complete/average/Ward), the dendrogram, and where to cut it. Why is it O(n^2) memory?

Three-panel scenario card. Panel one, the data: 3,000 customer profiles with 8 standardized features, a goal of 3 to 6 segments for marketing campaigns, and no fixed k known up front. Panel two, pilot runs and constraints: a single-linkage cut at k equal to 4 gave cluster sizes 2,991, 6, 2 and 1; a Ward run has six highest merge heights 52.0, 38.5, 31.0, 12.4, 10.9 and 9.7; next quarter the job must run on 100,000 customers on one 32 GB machine. Panel three lists the open decisions: which linkage, where to cut the dendrogram, and whether 100,000 points fit in 32 GB of RAM.

The Problem: A retail team wants 3 to 6 customer segments from 3,000 profiles with 8 standardized features, but a single-linkage pilot cut at k = 4 returned clusters of 2,991, 6, 2 and 1 customers, while a Ward run shows top merge heights of 52.0, 38.5, 31.0, 12.4, 10.9 and 9.7. Next quarter the same job must run on 100,000 customers on a 32 GB machine, and nobody has decided the linkage, the cut, or whether it will fit.

Answer

Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two closest clusters until one remains. Each merge and its height are recorded in a dendrogram, a binary tree with n-1 joins. The linkage defines “closest”: single uses the nearest cross pair, complete the farthest, average the mean over all cross pairs, and Ward the increase in within-cluster sum of squares. A flat clustering comes from cutting the tree horizontally, ideally inside a large gap between consecutive merge heights. The standard algorithm stores all n(n-1)/2 pairwise cluster distances and refreshes them after every merge with the Lance-Williams update. That matrix is the source of the O(n^2) memory, and at n = 100{,}000 it holds about 5 billion float64 values, or 40 GB.

(1) Single Linkage: Distance between the two nearest members, which makes the hierarchy equivalent to the minimum spanning tree. It recovers elongated, non-convex shapes but chains clusters together through a few bridging points.
(2) Complete Linkage: Distance between the two farthest members, so every cluster formed below height h has diameter at most h. Clusters come out compact, but one outlier inflates a diameter and large groups tend to be split.
(3) Average Linkage: Mean of all |A|\,|B| cross distances, also called UPGMA. It sits between the two extremes, dilutes single outliers, and works with any dissimilarity such as cosine or correlation.
(4) Ward Linkage: Merges the pair whose union adds the least within-cluster sum of squares, the same objective k-means minimizes. It yields compact clusters of similar size and only makes sense with Euclidean distance.
(5) Reading The Dendrogram: Only heights carry meaning, since the left-right order is one of 2^{n-1} equivalent flips. Cut at a fixed height, at the level that leaves k branches, or inside the largest gap between merge heights.
(6) Why Quadratic Memory: After merging i and j, the Lance-Williams update needs the old distances from every other cluster to both, so the whole distance matrix must stay live. The nearest-neighbor chain algorithm brings time down to O(n^2) from the naive O(n^3), but memory stays quadratic for complete and average linkage.

Mathematical Formulation:
d_{\mathrm{single}}(A,B) = \min_{a \in A,\, b \in B} d(a,b)
d_{\mathrm{complete}}(A,B) = \max_{a \in A,\, b \in B} d(a,b)
d_{\mathrm{avg}}(A,B) = \frac{1}{|A|\,|B|} \sum_{a \in A} \sum_{b \in B} d(a,b)
\Delta_{\mathrm{Ward}}(A,B) = \frac{|A|\,|B|}{|A| + |B|} \, \|\mu_A - \mu_B\|^2
d_{k,(ij)} = \alpha_i d_{ki} + \alpha_j d_{kj} + \beta d_{ij} + \gamma \delta_k
\delta_k = |d_{ki} - d_{kj}|
M = 8 \cdot \frac{n(n-1)}{2}\ \text{bytes}

Where:

  • A and B are two current clusters with members a and b, |A| is a cluster size, and d is the base dissimilarity. Single, complete and average linkage accept any dissimilarity.
  • \mu_A is the centroid of A, and \Delta_{\mathrm{Ward}} is the exact rise in total within-cluster SSE when A and B merge. It requires Euclidean geometry, and SciPy reports the merge height as \sqrt{2\Delta}.
  • d_{ki}, d_{kj} and d_{ij} are pre-merge distances between an untouched cluster k and the just-merged clusters i and j, and d_{k,(ij)} is the updated distance to their union.
  • \alpha_i, \alpha_j, \beta, \gamma select the linkage. Single uses \alpha = \frac{1}{2}, \beta = 0, \gamma = -\frac{1}{2} (a min), complete flips to \gamma = +\frac{1}{2} (a max), and average uses \alpha_i = \frac{n_i}{n_i + n_j} with \beta = \gamma = 0.
  • Ward uses \alpha_i = \frac{n_i + n_k}{n_i + n_j + n_k}, \beta = -\frac{n_k}{n_i + n_j + n_k} and \gamma = 0 on squared Euclidean distances, where n_i, n_j, n_k are cluster sizes.
  • M is the size of the condensed float64 distance matrix for n points: 36 MB at n = 3{,}000, 40 GB at n = 100{,}000, and double that for a full square matrix.

Worked Example:
On a line, take clusters A = \{0, 2\}, B = \{4, 9\} and C = \{12.5\}. The cross distances between A and B are 4, 9, 2 and 7, so single gives 2, complete gives 9 and average gives 5.5. Between B and C they are 8.5 and 3.5, so single gives 3.5, complete gives 8.5 and average gives 6.0. Single and average therefore merge A with B, while complete merges B with C because 8.5 beats 9. Ward compares SSE increases: \Delta(A,B) = 1 \times 5.5^2 = 30.25 and \Delta(B,C) = \frac{2}{3} \times 6^2 = 24, so Ward also picks B \cup C. Same three clusters, two different trees after one step.

Four scatter panels of the same 315 two-dimensional points, made of a large blob, a smaller blob joined to it by a thin bridge of 12 points, a curved arc above them, and three isolated points. Each panel colors the k equals 3 clustering from one linkage: single, complete, average and Ward, with the resulting cluster sizes printed in each panel title.

Figure 1: The same 315 points cut at k = 3 under each linkage, with cluster sizes in each panel title. Single linkage follows nearest-neighbor chains, so the 12-point bridge and the isolated points decide its partition. Ward prices every merge by added variance and favors compact groups of similar spread, and complete and average land between these extremes.

In practice the choice follows from two questions: which dissimilarity do you trust, and what cluster shape do you expect? Ward is the default for standardized numeric features where k-means-like segments are wanted. Average linkage is the default for non-Euclidean similarities such as correlation between gene-expression profiles, and single linkage is reserved for clean data with genuinely elongated structure.

PropertyWardAverageCompleteSingle
Merge criterionSmallest rise in within-cluster SSEMean of all cross-pair distancesFarthest cross pairNearest cross pair
Cluster shapeCompact, similar size and varianceModerately compact, tolerates size differencesCompact with bounded diameterArbitrary, elongated chains
Outliers and noiseAbsorbed into the nearest compact groupDiluted by averagingInflate diameters and split large groupsBecome singletons, and bridges chain clusters
DissimilarityEuclidean onlyAnyAnyAny
Best exact algorithmO(n²) time; O(nd) memory from stored centroidsNN-chain, O(n²) time and O(n²) memoryNN-chain, O(n²) time and O(n²) memoryMST or SLINK, O(n²) time and O(n) memory
Reach for it whenYou want k-means-like segments plus a hierarchyThe similarity is cosine or correlationEvery cluster must stay within a maximum spreadData are clean and shapes are non-convex

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *