Traditional models · lesson 9 of 16
Parts in this lesson
ADBSCAN(eps=0.2, min_samples=5)clusters as dense regions
BAgglomerativeClustering(2, linkage='single')merging the closest groups, again and again
CGaussianMixture(2).predict_proba(X)soft clusters: a probability for each
Dadjusted_rand_score(y, labels)how closely clusters match known groups

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):

clusters.ipynb

k-means scores 0.23. DBSCAN and single-linkage clustering both score 1.00, perfect. Look:

clusters.ipynb

k-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:

  1. A core point has at least min_samples points (itself included) within eps.
  2. Core points within eps of each other are in the same cluster, and so is every point within eps of one of them. A cluster spreads from core point to core point, along the data, whatever shape it takes.
  3. 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:

clusters.ipynb

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 k-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:

clusters.ipynb

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:

clusters.ipynb

k-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.

clusters.ipynb

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 k-means, maybe in a poor place, so scikit-learn can restart it (n_init). k-means is in fact a special case: EM with round, equal Gaussians and hard 0-or-1 responsibilities. Step through it:

EM stepsa mixture of two Gaussians
Fill: the chance a point belongs to the pink Gaussian (solid pink: certain; hollow: certainly not).

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 k-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?

MethodSees a cluster asNeedsGood for
k-means (4.8)a round blob around a centrekbig data, round groups of similar size
Gaussian mixturea stretched blob, with soft edgesthe number of clusterselliptical groups; probabilities of belonging
DBSCAN, HDBSCANa dense region, any shapeeps and min_samplesodd shapes, noise, an unknown number of groups
Agglomerativea branch of a treea linkage, then where to cutsmall 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

Which method can find two interlocking crescent-shaped clusters, and mark stray points as noise?
A Gaussian mixture says a customer belongs to group A with probability 0.55 and group B with 0.45. What does k-means say?
In EM for a Gaussian mixture, what happens in the M step?

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.

All sources for Simple Machine Learning