Stop 11 · Unsupervised LearningTopics · 5/10

Gaussian Mixture Models

Part of the Machine Learning Roadmap

In short

K-means draws hard borders and assumes round clusters. Real groups are often stretched ovals that overlap. A Gaussian mixture model handles both, and tells you how sure it is about every point.

A Gaussian mixture model assumes data comes from several overlapping bell-shaped clusters and gives each point a probability of belonging to each one. It handles stretched, oval clusters and soft assignments better than k-means.

The jaadu leans over a white tablecloth studying three overlapping oval tea stains, with three tipped-over cups lying beside them.

Think of it like this

Overlapping chai stains

Spill three cups of chai on a tablecloth and you get three stains: each darkest at the centre and fading outwards, some stretched into ovals, some overlapping. Point at a spot in the overlap and you can say '70% from cup one, 30% from cup two'. A GMM models data as overlapping stains like these and gives each point a share in each one.

Watch it happen

K-means vs Gaussian mixture

K-means

  • Hard assignment: each point belongs to exactly one cluster.
  • Assumes round clusters of similar size.
  • Fast and simple. A great default.

Gaussian mixture

  • Soft assignment: probability of each cluster for every point.
  • Learns each cluster's centre, shape, tilt and size.
  • Can compare models with BIC to help choose the number of components.

Use a GMM when clusters are stretched or overlapping, or when you need to know how confident each assignment is.

The big ideas

What you need to know

A mix of bell curves

The data is modelled as several Gaussians, each with a mean, a covariance (shape) and a weight (share of points).

Fitted with EM

Expectation-maximisation alternates: estimate each point's membership probabilities, then update each Gaussian. Repeat until stable.

Soft assignments

predict_proba gives each point's probability for every component. Borderline points get split probabilities.

Choose components with BIC

Lower BIC balances fit against complexity. Fit for several n_components and pick the lowest.

See it in code

Walk through the code

python
1import numpy as np2from sklearn.datasets import make_blobs3from sklearn.mixture import GaussianMixture4 5X, _ = make_blobs(n_samples=600, centers=3, random_state=170)6X = X @ np.array([[0.6, -0.6], [-0.4, 0.8]])        # stretch blobs into tilted ovals7 8bics = {}9for n in range(1, 7):10    gm = GaussianMixture(n_components=n, covariance_type="full", random_state=0).fit(X)11    bics[n] = gm.bic(X)12best_n = min(bics, key=bics.get)13print("Best number of components:", best_n)14 15gmm = GaussianMixture(n_components=best_n, covariance_type="full", random_state=0).fit(X)16labels = gmm.predict(X)17probs = gmm.predict_proba(X)18 19unsure = np.where(probs.max(axis=1) < 0.8)[0]20print(len(unsure), "points are less than 80% sure of their cluster")

1/4Three blobs, then a matrix multiply that stretches and tilts them into ovals.

Stretched, tilted clusters that k-means splits badly, handled by a full-covariance GMM.

Save yourself the pain

Common mistakes

Oops

Using covariance_type='spherical' and wondering why ovals are split badly.

Do this instead

Use 'full' to let each component learn its own shape and tilt, if you have enough data.

Oops

Treating predict's hard labels as the whole story.

Do this instead

Look at predict_proba. A point at 51% vs 49% is not the same as one at 99%.

Your mission

K-means vs GMM showdown

~30 min

Quick check

What does a GMM give you that k-means does not?

Go deeper

  1. 1Fit GaussianMixture on 2D data and plot the probability of each point's cluster.
  2. 2Compare its clusters with k-means on elongated data.
  3. 3Use BIC to choose the number of components.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge