#hierarchical-clustering — Unsupervised learning
Merge the points two at a time until only one is left: the dendrogram, the linkage criteria, and the cut height that decides the number of clusters.
What you'll play with
- Welcome to #hierarchical-clustering. On the left, 30 grey points on a plane: you can guess three blobs, but the machine does not know that yet — each point is its own cluster. On the right, an empty dendrogram: 30 leaves in a row, a height axis and, all the way up, a yellow cut plane. The idea fits in one sentence: merge the two closest clusters, again and again, until only one is left. Each merge draws a "U" whose height is the distance at which it happened.
- Trigger the first merge:
/merge. The algorithm scans the distance matrix for the closest pair and welds them. - Another one:
/merge. Compare the height of the new U with the first one. - Let it run to the end:
/run. The 29 merges chain together, from pairs to groups, up to a single root. - The yellow plane is above everything: a single cluster. Push it down:
/cut 1.5, then count the branches it crosses. - At 1.5 it works, but that is a bit lucky. Go the other way around: directly ask for three groups with
/clusters 3. The plane places itself in the middle of the biggest gap between two merges. - New terrain:
/dataset chain. A line of points at regular intervals, and a small compact group next to it. Look at how average linkage cuts this at the current cut height. - Switch to single linkage:
/linkage single. The distance between two groups becomes the distance between their two closest points. - Your turn:
/linkage wardthen/clusters 2on the line;/dataset ringsthen/linkage singleand/clusters 2(the two rings recovered) versus/linkage complete(failure);/n 60to densify;/seed 12for other points;/undoto reverse a merge;/resetto start over. Next: #dbscan, which finds groups by density without fixing their number, and #k-means, the rival that must know k in advance.
Channel commands
/merge— Perform the next merge: the two closest clusters weld together./run— Run every remaining merge: the full tree, up to the root./undo— Undo the last visible merge./linkage <single|complete|average|ward>— Change the linkage criterion and recompute the whole tree (same number of visible merges)./cut <height=0..4>— Place the cut plane at this height: as many clusters as crossed branches./clusters <m=1..10>— Pick the number of clusters: the plane lands in the middle of the right height gap./dataset <blobs|chain|rings>— Change the dataset (same n, same seed) and recompute the tree./n <10..60>— Change the number of points and recompute the tree./seed <1..99>— Redraw the points with another seed (same dataset, same n)./reset— Back to the start: blobs, 30 points, average linkage, no merge, cut plane all the way up.
Glossary
- Agglomerative hierarchical clustering
- Unsupervised grouping method that starts with n one-point clusters and, at each step, merges the two closest clusters, until only one is left. You get an entire hierarchy of partitions instead of a single one.
- Dendrogram
- Tree that summarises every merge: the leaves are the points, each "U" links two clusters at the height of their merge. Cutting it at a given height yields a partition into clusters.
- Linkage criterion
- Rule that defines the distance between two clusters from the point-to-point distances: single, complete, average, Ward… It decides the merge order and therefore the shape of the tree.
- Single linkage
- Distance between two clusters = distance between their two closest points. Follows elongated shapes and rings, but suffers from the chaining effect.
- Complete linkage
- Distance between two clusters = distance between their two farthest points, that is, the diameter of the merged cluster. Produces compact, round clusters, but chops elongated shapes.
- Average linkage
- Distance between two clusters = mean of every point-to-point distance between the two groups. A compromise between single and complete, robust to outliers.
- Ward's method
- Criterion that merges the two clusters whose union increases the intra-cluster inertia the least (the sum of squared distances to the centroids). Favours compact clusters of comparable sizes; it is scikit-learn's default criterion.
- Cut height
- Distance threshold at which you slice the dendrogram: the crossed branches become the clusters. Cutting through the middle of the largest height jump gives the most robust partition; you can also target a specific number of clusters directly.
- Chaining effect
- The pitfall of single linkage: a line of close points acts as a bridge and, step by step, merges groups that have nothing to do with each other. The dendrogram flattens at the bottom and no clean gap is left to cut through.
- Distance matrix
- The n × n table of distances between every pair of points, the starting point of the algorithm. After each merge, the row of the new cluster follows from the old ones via the Lance-Williams formula.
Other channels in Unsupervised learning
- #k-means — Grouping without labels: centroids that move, inertia that drops, the choice of k — and the shapes where k-means fails.
- #pca — Principal component analysis: find the axes where data varies most, project, compress — and measure what is lost.
- #hierarchical-clustering — Merge the points two at a time until only one is left: the dendrogram, the linkage criteria, and the cut height that decides the number of clusters.
- #dbscan — Group by density: epsilon, MinPts, core, border and noise points — the algorithm that finds arbitrary shapes and ignores intruders.
- #anomaly-detection — Spot what fits nothing: z-score / Mahalanobis, Isolation Forest, LOF — three ways to say 'this point is odd'.
- #t-sne-umap — Map the high dimensions: t-SNE and UMAP unfold 10-dimensional data into a readable 2D map — perplexity, neighbors, and reading pitfalls.