0
0
ML Pythonml~5 mins

Gaussian Mixture Models in ML Python

Choose your learning style9 modes available
Introduction
Gaussian Mixture Models help us find groups in data by assuming each group looks like a bell curve. This helps us understand and organize data better.
When you want to find hidden groups in customer data to offer personalized deals.
When you want to separate different sounds in an audio recording.
When you want to detect unusual patterns, like fraud in bank transactions.
When you want to simplify complex data by grouping similar points together.
When you want to model data that comes from multiple sources mixed together.
Syntax
ML Python
from sklearn.mixture import GaussianMixture

gmm = GaussianMixture(n_components=number_of_groups, covariance_type='full')
gmm.fit(data)
predictions = gmm.predict(data)
n_components is how many groups (bell curves) you want to find.
covariance_type controls the shape of each group; 'full' means each group can have any shape.
Examples
Finds 3 groups in data X and predicts which group each point belongs to.
ML Python
gmm = GaussianMixture(n_components=3)
gmm.fit(X)
predictions = gmm.predict(X)
Finds 2 groups assuming each group has diagonal-shaped spread.
ML Python
gmm = GaussianMixture(n_components=2, covariance_type='diag')
gmm.fit(X)
predictions = gmm.predict(X)
Finds 4 groups with a fixed random seed for reproducible results.
ML Python
gmm = GaussianMixture(n_components=4, random_state=42)
gmm.fit(X)
predictions = gmm.predict(X)
Sample Model
This program creates two groups of points, fits a Gaussian Mixture Model to find these groups, and prints how many points belong to each group and the first five group predictions.
ML Python
import numpy as np
from sklearn.mixture import GaussianMixture

# Create sample data with 2 groups
np.random.seed(0)
group1 = np.random.normal(loc=0, scale=1, size=(100, 2))
group2 = np.random.normal(loc=5, scale=1, size=(100, 2))
data = np.vstack([group1, group2])

# Create and fit GMM with 2 groups
model = GaussianMixture(n_components=2, random_state=0)
model.fit(data)

# Predict group for each point
labels = model.predict(data)

# Print how many points in each group
unique, counts = np.unique(labels, return_counts=True)
print(f"Group counts: {dict(zip(unique, counts))}")

# Print first 5 predictions
print(f"First 5 group predictions: {labels[:5]}")
OutputSuccess
Important Notes
Gaussian Mixture Models can find overlapping groups because they use probabilities.
Choosing the right number of groups (n_components) is important and can be done by testing different values.
The model assumes data in each group follows a bell curve shape.
Summary
Gaussian Mixture Models find groups in data by assuming each group looks like a bell curve.
They work well when groups overlap or have different shapes.
You can use them to organize data, detect patterns, or find hidden groups.