ML Notes
Master Gaussian Mixture Models: probabilistic clustering, soft assignments, EM algorithm, covariance types, model selection, implementation with scikit-learn.
What are Gaussian Mixture Models?
Gaussian Mixture Models (GMM) assume data is generated from multiple Gaussian distributions. Unlike K-Means (hard assignment), GMM provides soft assignments (cluster membership probabilities).
GMM vs K-Means
| K-Means: Each point | one cluster (hard assignment) |
| Output | [1, 0, 0] - point definitely in cluster 1 |
| GMM: Each point | all clusters (soft assignment) |
| Output | [0.8, 0.15, 0.05] - 80% cluster 1, 15% cluster 2, 5% cluster 3 |
GMM Components
Each component k is a Gaussian:
N(μₖ, Σₖ) where:
μₖ = cluster mean
Σₖ = covariance matrix
πₖ = component weight (mixing coefficient)
EM Algorithm (Parameter Estimation)
E-Step (Expectation)
Compute responsibility (probability point belongs to each cluster):
γᵢₖ = P(k|xᵢ) = (πₖ * N(xᵢ|μₖ, Σₖ)) / (Σⱼ πⱼ * N(xᵢ|μⱼ, Σⱼ))
M-Step (Maximization)
Update parameters based on responsibilities:
μₖ = (Σᵢ γᵢₖ * xᵢ) / Σᵢ γᵢₖ
Σₖ = (Σᵢ γᵢₖ * (xᵢ - μₖ)ᵀ(xᵢ - μₖ)) / Σᵢ γᵢₖ
πₖ = (1/N) * Σᵢ γᵢₖ
Iteration
Repeat E-step and M-step until convergence (log-likelihood change < threshold).
Covariance Types
Scikit-learn supports different covariance matrix structures:
covariance_type = 'full' # Full covariance (most flexible, most parameters)
'tied' # All components share covariance (fewer params)
'diag' # Diagonal covariance (features independent)
'spherical' # Identity covariance (most constrained, spheres)Implementation with Scikit-Learn
from sklearn.mixture import GaussianMixture
from sklearn.datasets import make_blobs
import numpy as np
# Generate synthetic data
X, y_true = make_blobs(n_samples=300, centers=4, random_state=42)
# Fit GMM
gmm = GaussianMixture(n_components=4, covariance_type='full', random_state=42, n_init=10)
gmm.fit(X)
# Predictions
# Hard assignment (most likely cluster)
labels = gmm.predict(X)
# Soft assignment (probabilities)
probabilities = gmm.predict_proba(X)
print(f"BIC: {gmm.bic(X)}")
print(f"AIC: {gmm.aic(X)}")
print(f"Log-likelihood: {gmm.score(X)}")
# Access parameters
print(f"Means shape: {gmm.means_.shape}")
print(f"Covariances shape: {gmm.covariances_.shape}")
print(f"Weights: {gmm.weights_}")Model Selection (Finding Optimal K)
1. BIC (Bayesian Information Criterion)
2. AIC (Akaike Information Criterion)
3. Scree Plot
Advantages
- Soft Assignments: Probabilities instead of hard clusters
- Probabilistic Framework: Likelihood-based model selection (BIC, AIC)
- Flexibility: Full covariance captures complex shapes
- Uncertainty Quantification: Know confidence in assignments
- Handles Elliptical Clusters: Unlike K-Means (spheres only)
Disadvantages
- Complexity: More parameters than K-Means
- Overfitting Risk: Full covariance needs sufficient data
- Computational Cost: EM algorithm slower than K-Means
- Initialization Sensitive: Multiple initializations recommended
- Singularity Issues: Can fail with bad initialization or insufficient data
Comparison with Other Methods
| Aspect | GMM | K-Means | DBSCAN | Hierarchical |
|---|---|---|---|---|
| Assignment | Soft (prob) | Hard | Hard | Hard |
| Probabilistic | Yes | No | No | No |
| Model Selection | BIC/AIC | Elbow | Epsilon tuning | Dendrogram |
| Outlier Handling | Probability | Included | Automatic | Included |
| Scalability | Moderate | Fast | Moderate | Slow |
Practical Example: Full Workflow
When to Use GMM
Use GMM when:
- Soft assignments needed (confidence scores important)
- Probabilistic framework required
- Model selection via likelihood critical
- Elliptical/varied-size clusters
- Uncertainty quantification needed
Use K-Means when:
- Hard assignments sufficient
- Speed critical (GMM slower)
- Computational resources limited
- Interpretability over flexibility
Use DBSCAN when:
- Arbitrary cluster shapes needed
- Outlier detection automatic
- Don't know number of clusters
Quick Revision Notes
- GMM probabilistic, soft assignments
- EM Algorithm iteratively optimizes parameters
- BIC/AIC model selection (lower is better)
- Covariance Types trade-off complexity vs flexibility
- Full Covariance most flexible but needs more data
- Soft Assignments provide confidence scores
- Singular Matrices watch for numerical issues with full covariance
Interview Q&A
Q1: Explain soft vs hard assignments in GMM.
A: Hard assignment (K-Means): point assigned to exactly one cluster [1, 0, 0]. Soft assignment (GMM): point has probability for each cluster [0.7, 0.2, 0.1]. Soft assignments provide uncertainty, confidence scores. Useful when membership ambiguous.
Q2: Why use BIC/AIC for GMM model selection?
A: Both penalize complexity preventing overfitting. BIC stronger penalty for large datasets. Lower BIC/AIC better. Can compare different K and covariance types. Principled alternative to elbow method.
Q3: When would you choose GMM over K-Means?
A: Need soft assignments (probabilities), probabilistic framework for inference, model selection via likelihood, varying cluster sizes/shapes. K-Means simpler, faster. GMM more flexible but complex.
Q4: Challenges in EM algorithm?
A: Can get stuck in local optima (use multiple initializations). Singular covariance matrices (add regularization). Slow convergence (limit iterations). Oversmoothing with wrong K.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Gaussian Mixture Models (GMM): Probabilistic Clustering for Advanced Data Analysis.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, clustering, gaussian, mixture, models
Related Machine Learning Topics