Other clusters: DBSCAN, hierarchical, Gaussian mixtures
Last updated
k-means sees every cluster as a round blob around a centre. Real groups can be long, curved, nested inside each other, or have no clear edge. This lesson meets three methods that see differently: DBSCAN follows dense regions wherever they go, hierarchical clustering builds a family tree of groups, and Gaussian mixtures give each point a probability for each group, fitted by expectation–maximisation.
By the end of this lesson you can
- Find clusters of any shape, and noise, with DBSCAN
- Build and read a dendrogram
- Fit a Gaussian mixture, and explain the E and M steps
- Pick a clustering method for the shape of the data
Wrong way: k-means on two moons
The two moons are as clear as clusters get: two crescents, well apart. Ask for two clusters, three ways, and score each against the true moons with the adjusted Rand index (1 means the same grouping as the truth, 0 no better than chance):
-means scores 0.23. DBSCAN and single-linkage clustering both score 1.00, perfect. Look:
-means cut straight across both moons. It can't do anything else: every point goes to its nearest centre, so the border between two clusters is always the straight line halfway between their centres (lesson 4.8). Two centres can never describe two interlocking crescents. The data was fine; the method's idea of a cluster didn't fit it.
DBSCAN: clusters as dense regions
DBSCAN (Ester and others, 1996) defines a cluster as a region where points are packed close together. With two settings, a radius eps and a count min_samples:
- A core point has at least
min_samplespoints (itself included) withineps. - Core points within
epsof each other are in the same cluster, and so is every point withinepsof one of them. A cluster spreads from core point to core point, along the data, whatever shape it takes. - Points that aren't near any core point are noise, labelled −1.
It finds the number of clusters itself, and it marks outliers instead of forcing them into a group. Its weakness is eps:
Too small (0.05), and most points are noise, scattered in ten tiny clusters. Too big (0.5), and the moons merge into one. Only 0.2 gets two clusters and no noise. And one eps serves the whole data, so DBSCAN struggles when some clusters are much denser than others; HDBSCAN, also in scikit-learn, allows for that.
Hierarchical clustering and dendrograms
Agglomerative (bottom-up) clustering starts with every point as its own cluster, then merges the two closest clusters, again and again, until there's one. "Closest" is the linkage:
- single: the closest pair of points between the two clusters. Follows long, thin shapes, like the moons, but can chain two clusters together through a bridge of points.
- complete: the furthest pair. Compact clusters.
- ward (the default): the merge that increases the total squared distance to the centres least, like -means.
The whole history of merges is a dendrogram, a tree with the points as leaves and the height of each join showing how far apart the groups were when merged:
Eighteen points, made as three groups of six. Low joins inside each group, then two big jumps. Cut the tree with a horizontal line below the big jumps and you get three clusters; cut higher and you get two. You choose the number after seeing the tree, which is its great advantage. Its cost: it compares every pair of points, so memory grows with the square of the rows. Tens of thousands of rows are the practical limit.
Soft clusters: Gaussian mixtures
Two long, thin clouds, one just above the other:
-means scores 0.0: the clouds are long, so their left halves are closer to each other than to their own right halves, and it cuts them left and right. A Gaussian mixture scores 0.97. It models each cluster as a bell curve in 2-D (lesson 1.10) with its own mean and its own covariance: how stretched it is, and in which direction. A long, flat cluster is no problem.
It also gives soft answers: a probability of belonging to each cluster.
The point (0, 0), in the middle of the lower cloud: certainly cluster 0. (0, 2), in the upper cloud: certainly 1. (0, 1), exactly between: 51% and 49%. A hard clustering would have to pretend it knew.
Expectation–maximisation
If you knew which cluster each point came from, fitting each Gaussian would be easy: the mean and covariance of its points. If you knew the Gaussians, you could work out which cluster each point probably came from. Expectation–maximisation (EM; Dempster, Laird and Rubin, 1977) does both in turn:
- E step: for each point, the probability it came from each Gaussian, its responsibilities, given the current Gaussians.
- M step: refit each Gaussian, its weight, mean and covariance, using every point weighted by its responsibility.
Each round makes the data more likely under the mixture, so EM always settles, though, like -means, maybe in a poor place, so scikit-learn can restart it (n_init). -means is in fact a special case: EM with round, equal Gaussians and hard 0-or-1 responsibilities. Step through it:
The start: two round Gaussians, left and right, where k-means would put them, and every point 50–50. Move the slider: each round is an E step then an M step.
Started left and right, where -means would put its centres, the two Gaussians stretch sideways and stay there: each covers half of both clouds, and no round of EM can move it, because every small change makes the fit worse. That's the "poor place". Started one above the other, they stretch along the two clouds and settle there. Real libraries don't rely on one start: scikit-learn's GaussianMixture can run several (n_init) and keep the most likely, which is what step 4's notebook did.
Which method?
The same question as for any model: what do your clusters look like?
| Method | Sees a cluster as | Needs | Good for |
|---|---|---|---|
| -means (4.8) | a round blob around a centre | big data, round groups of similar size | |
| Gaussian mixture | a stretched blob, with soft edges | the number of clusters | elliptical groups; probabilities of belonging |
| DBSCAN, HDBSCAN | a dense region, any shape | eps and min_samples | odd shapes, noise, an unknown number of groups |
| Agglomerative | a branch of a tree | a linkage, then where to cut | small data; groups within groups |
All of them are built on distances, so scale the features first (lesson 4.1). And none can tell you the groups are meaningful. Clustering always returns something; check the groups against what you know before acting on them.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- scikit-learn: DBSCAN: DBSCAN (Ester, Kriegel, Sander and Xu, KDD 1996), its eps and min_samples
- Dempster, Laird and Rubin, Maximum likelihood from incomplete data via the EM algorithm, Journal of the Royal Statistical Society B 39(1), 1977: expectation–maximisation
- scikit-learn: Clustering: DBSCAN, AgglomerativeClustering, and a comparison of methods on toy data
- scikit-learn: Gaussian mixture models: GaussianMixture and EM