MODELCATALOG

Soft clustering with probabilities.

sklearn.mixture

mixture

Soft clustering with probabilities.

1 Get data Something to practise on
2 Split & tune An honest test set
3 Prepare Into numbers a model accepts
4 Compose One object, no leakage
5 Model The algorithm itself
6 Unsupervised No labels available
7 Measure Is it any good, and why
8 Utilities Glue and extension

What it is for

Models the data as a blend of Gaussian distributions, so each point gets a probability of belonging to each component rather than a hard assignment.

Contents

GaussianMixture You specify the number of components
BayesianGaussianMixture Infers how many are needed

Worked examples 1

Runnable, with the arguments that change the outcome called out. Output shapes are shown as comments where they help.

Soft clustering with probabilities

Where KMeans gives a hard assignment, a Gaussian mixture gives a probability per component — so you can see which rows are genuinely ambiguous.

from sklearn.mixture import GaussianMixture
import numpy as np

gm = GaussianMixture(n_components=4, covariance_type="full",
                     n_init=5, random_state=42).fit(X_scaled)

proba = gm.predict_proba(X_scaled)
labels = gm.predict(X_scaled)

# Rows where the top probability is low sit between segments.
# In customer segmentation those are often the interesting ones.
confidence = proba.max(axis=1)
print("ambiguous rows:", (confidence < 0.6).sum())

# BIC chooses the number of components without needing labels
for k in range(2, 8):
    g = GaussianMixture(k, random_state=42).fit(X_scaled)
    print(f"k={k}  BIC={g.bic(X_scaled):12.0f}")   # lower is better