{"slug": "hierarchical-clustering-fails-beautifully", "title": "Hierarchical Clustering Fails Beautifully", "summary": "An SRE's exploration of hierarchical clustering reveals that while the dendrogram output looks convincingly like discovered truth, it can be completely wrong when built with the wrong linkage on flat data. The engineer notes that Ward's method greedily minimizes within-cluster sum of squares, similar to K-Means, but is restricted to Euclidean distance, and that single linkage can be understood through a minimum spanning tree.", "body_md": "*Classic Machine Learning Through the Eyes of an SRE — Part 8*\n\nThe most dangerous output in my whole Week-1 study set wasn't a bad prediction. It was a beautiful tree.\n\nHierarchical clustering produces a dendrogram, that elegant diagram where every account, ticket, or incident nests inside ever-larger families. It looks like discovered truth. Stakeholders lean in. Someone screenshots it for the QBR deck.\n\nNothing else in the set looks as convincing while being as capable of being completely wrong. A bad K-Means gives you blobs that feel arbitrary, and people push back. A dendrogram built with the wrong linkage on flat data still looks like a family tree of your business.\n\nNobody pushes back on a tree.\n\n**The bet and the build**\n\nHierarchical clustering completes the answer-finding taxonomy I've been using through this series. That's my own shorthand, not standard terminology: K-Means SEARCHES, DBSCAN DEFINES, PCA SOLVES, and hierarchical clustering BUILDS.\n\nStart with every point as its own cluster. Repeatedly merge the closest two clusters. Never undo. Greedy and irreversible, a little like growing a decision tree. Same skeleton, different family.\n\nThere is also a top-down version, called divisive clustering, which starts with everything together and splits it. In practice, when people say hierarchical clustering, they're usually talking about the bottom-up, agglomerative version.\n\nTwo things were genuinely new to me.\n\n**You choose the cut after seeing the structure.** Fitting doesn't require you to decide K upfront. The dendrogram gives you the hierarchy, and you choose where to cut it to get the number of clusters you want. That makes the output unusually flexible. For a delivery organization it also feels natural, because account family → sub-segment → individual account is already how a lot of governance gets organized.\n\n**Linkage is a selectable worldview.** \"Closest clusters\" needs a definition, and every definition makes a different assumption. Ward pushes toward compact, variance-minimizing clusters. Single linkage is comfortable with chains. Complete linkage favors tighter groups. You aren't configuring a minor implementation detail. You're choosing what kind of structure the tree is allowed to see.\n\nSingle linkage had another connection I immediately recognized. It can be understood through a minimum spanning tree: connect the points through the cheapest edges, then cut the longest ones. Once I saw that, chaining stopped being a quirk to memorize. A thin bridge of intermediate points can connect two otherwise separate groups, because the algorithm only needs those cheap nearest-neighbor links. That's a graph algorithm I already knew, wearing a clustering costume. The same thing happened in Part 6, where DBSCAN turned out to have the skeleton of connected components.\n\n**The subtlety that took me three rounds**\n\nWhat does hierarchical clustering optimize? My first answer was \"nothing, like DBSCAN.\" Wrong. The optimization story is hybrid.\n\nAgglomerative clustering doesn't optimize one global objective across every possible tree. It makes greedy merges according to the linkage you selected. Ward is the interesting case: each merge is chosen to minimize the increase in within-cluster sum of squares, the same quantity underlying the K-Means objective. Same objective family, different strategy. K-Means searches for a solution, Ward builds one greedily, and neither guarantees the globally optimal result.\n\nOne important restriction: Ward is tied to Euclidean distance. If you're thinking about cosine similarity or another non-Euclidean distance, Ward isn't the appropriate choice, and current implementations explicitly restrict it to Euclidean/L2.\n\nThat is when \"what's the loss?\" stopped being a checkbox question for me. The answer can be no single global objective, a linkage-specific criterion, or a greedy local criterion, and each one creates a different failure mode.\n\n**The part of the picture that isn't information**\n\nHere is the thing that changed how I read every dendrogram since.\n\n**The left-to-right position of the leaves is not a similarity measure**. At every merge, the two child subtrees can be placed on either side without changing the underlying clustering, so the same hierarchy has many valid visual arrangements. What carries information is the vertical axis: how far apart the groups were when they merged.\n\nBut that's not how people read it. They scan a dendrogram horizontally, like a spectrum, and conclude that neighboring leaves are similar. Two accounts sitting side by side at the bottom may not be similar at all. They may only merge near the top of the tree, which is the algorithm telling you they are far apart. Libraries even provide leaf-ordering options to make the visual structure more intuitive without changing the clustering.\n\nSo the most persuasive axis on the most persuasive chart carries no similarity information at all.\n\nThat's what \"fails beautifully\" actually means.\n\n**The two checks worth running**\n\nTwo useful checks tell you whether a beautiful tree deserves more scrutiny.\n\n**Cophenetic correlation** compares the original pairwise distances with the distances represented by the dendrogram. High correlation means the tree preserves the pairwise geometry reasonably well. Low means you're looking at a substantial distortion.\n\n**Cluster stability** means resampling the data, refitting, and checking whether the clusters survive. If the membership changes substantially across resamples, the structure isn't robust enough to treat as established fact.\n\nA dendrogram that fails both is still gorgeous. That's the trap.\n\nTwo more practical notes. Distances are scale-sensitive, so standardize before fitting, the same rule that applied to K-Means, SVM and PCA earlier in the series. And when candidate merges tie, implementation and input ordering can affect which merge is selected, so two implementations can produce different trees from the same data. Part 6 had a version of this same lesson with DBSCAN border points, and I keep relearning it: \"deterministic\" usually has an asterisk.\n\n**The wall this hits**\n\nAn n-by-n distance matrix is O(n²) memory. Depending on the linkage and implementation, agglomerative clustering can range from O(n²) to O(n³) time. SciPy documents O(n²) implementations for several common linkages and O(n³) for some others, with O(n²) memory across these implementations.\n\nFine for two hundred accounts. Not so fine for two hundred thousand tickets.\n\nA mistake I made while studying and want to keep visible: I originally listed the scale wall as a trust check. It isn't. It tells you whether hierarchical clustering can run at all, not whether the answer is right. Different question entirely.\n\n**What I'd tell my ops team**\n\nUse hierarchical clustering when the domain is genuinely nested and reasonably small: account taxonomies, competency trees, incident-catalog dedup. Pick linkage deliberately, because it is your shape hypothesis, not a default. Run both checks before any stakeholder sees the tree. Never let anyone read left-to-right adjacency as similarity, and say that explicitly when you present it.\n\nAnd put the dendrogram last in the deck, after the caveats. Because the moment it appears, the room stops evaluating and starts believing.\n\nThe convincing failure is the expensive one.\n\n**Production takeaway**\n\nRun both checks before a stakeholder sees the dendrogram: cophenetic correlation and cluster stability across resamples. Choose linkage deliberately. Standardize first. And know the scale wall, because O(n²) memory can become the constraint long before the business question gets interesting.\n\n**Common interview mistake**\n\nTwo. First, saying hierarchical clustering optimizes nothing. The optimization story is hybrid: agglomerative clustering is greedy and linkage-driven, while Ward chooses each merge by minimizing the increase in within-cluster variance, related directly to the K-Means objective.\n\nSecond, treating the horizontal position of leaves as a similarity measure. It isn't. The tree's merge heights carry the distance information. Leaf ordering is primarily a visualization choice.\n\n**Where I'd use this in a real production system**\n\nSmall, genuinely nested domains: account taxonomies, competency trees, incident-catalog dedup. Hundreds of items, not hundreds of thousands.\n\n*Classic Machine Learning Through the Eyes of an SRE. What each algorithm bets about your world, read through a production lens. Previous: PCA. Final part: Isolation Forest, the anomaly detector that builds isolation into the algorithm rather than optimizing a conventional loss.*", "url": "https://wpnews.pro/news/hierarchical-clustering-fails-beautifully", "canonical_source": "https://dev.to/nishant_banginwar_80b7dc5/hierarchical-clustering-fails-beautifully-561b", "published_at": "2026-08-25 03:16:02+00:00", "updated_at": "2026-08-25 03:43:04.143863+00:00", "lang": "en", "topics": ["machine-learning"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/hierarchical-clustering-fails-beautifully", "markdown": "https://wpnews.pro/news/hierarchical-clustering-fails-beautifully.md", "text": "https://wpnews.pro/news/hierarchical-clustering-fails-beautifully.txt", "jsonld": "https://wpnews.pro/news/hierarchical-clustering-fails-beautifully.jsonld"}}