◐APLab.academy
КурсыИнструментыPremium
··
Войти
APLAB.ACADEMY © 2026 · BUILT BY AP LAB
КУРСЫИНСТРУМЕНТЫКОНФИДЕНЦИАЛЬНОСТЬУСЛОВИЯ
ADVANCED ML: UNSUPERVISED LEARNING & PRODUCTION / L04 — GAUSSIAN MIXTURE MODELS & EM ALGORITHM04 / 12 · ██████░░░░░░░░░░░░░░ 33%
УРОКИ · 12
01Clustering Algorithms: K-Means, DBSCAN, Hierarchical02Dimensionality Reduction: PCA, t-SNE, UMAP03Anomaly Detection: Isolation Forest & One-Class SVM04Gaussian Mixture Models & EM Algorithm05Neural Networks Fundamentals: Perceptrons to Backpropagation06Deep Learning Basics: Architectures & Training07Time Series Analysis: ARIMA, Prophet, ML Forecasting08Reinforcement Learning Introduction: Q-Learning & Agents09Model Deployment: API Design & Serving Infrastructure10MLOps Fundamentals: Pipelines, Monitoring, Versioning11Model Optimization: Quantization, Pruning, Distillation12Production Best Practices: A/B Testing, Drift, Debugging
НА ЭТОЙ СТРАНИЦЕ
Introduction: Soft Clustering with ProbabilitiesLearning Objectives1. From Gaussian to MixtureSingle Gaussian DistributionMixture of Gaussians2. The EM AlgorithmE-Step (Expectation)M-Step (Maximization)
УРОКИ · 12 · 04 / 12▾
01Clustering Algorithms: K-Means, DBSCAN, Hierarchical02Dimensionality Reduction: PCA, t-SNE, UMAP03Anomaly Detection: Isolation Forest & One-Class SVM04Gaussian Mixture Models & EM Algorithm05Neural Networks Fundamentals: Perceptrons to Backpropagation06Deep Learning Basics: Architectures & Training07Time Series Analysis: ARIMA, Prophet, ML Forecasting08Reinforcement Learning Introduction: Q-Learning & Agents09Model Deployment: API Design & Serving Infrastructure10MLOps Fundamentals: Pipelines, Monitoring, Versioning11Model Optimization: Quantization, Pruning, Distillation12Production Best Practices: A/B Testing, Drift, Debugging
LESSON 04 · ADVANCED · 60 MIN · ◆ 1 INSTRUMENT

Gaussian Mixture Models & EM Algorithm

Probabilistic clustering with GMMs. Understand the Expectation-Maximization algorithm and Bayesian approaches to clustering.

Introduction: Soft Clustering with Probabilities

Remember K-Means? It assigns each point to exactly ONE cluster – a hard assignment. But what if a point is right between two clusters? What if clusters overlap?

Gaussian Mixture Models (GMMs) solve this with soft clustering: each point can belong to multiple clusters with different probabilities!

Key Insight: GMMs model data as a mixture of Gaussian distributions, providing probabilistic cluster assignments and enabling anomaly detection, density estimation, and generative modeling.

Learning Objectives

  • Understand Gaussian distributions and mixture models
  • Master the Expectation-Maximization (EM) algorithm
  • Compare GMMs with K-Means
  • Use GMMs for anomaly detection
  • Apply GMMs to real-world clustering problems
  • Handle model selection (choosing number of components)

1. From Gaussian to Mixture

Single Gaussian Distribution

A Gaussian (normal) distribution is defined by:

  • Mean μ\muμ: center
  • Covariance Σ\SigmaΣ: spread and orientation

Probability density:

N(x∣μ,Σ)=1(2π)d/2∣Σ∣1/2exp⁡(−12(x−μ)TΣ−1(x−μ))\mathcal{N}(x | \mu, \Sigma) = \frac{1}{(2\pi)^{d/2}|\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu)\right)N(x∣μ,Σ)=(2π)d/2∣Σ∣1/21​exp(−21​(x−μ)TΣ−1(x−μ))
FIG. 02Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive Python code execution environment

Mixture of Gaussians

A mixture is a weighted sum of multiple Gaussians:

p(x)=∑k=1KπkN(x∣μk,Σk)p(x) = \sum_{k=1}^{K} \pi_k \mathcal{N}(x | \mu_k, \Sigma_k)p(x)=k=1∑K​πk​N(x∣μk​,Σk​)

Where:

  • KKK = number of components
  • πk\pi_kπk​ = mixing coefficient (weight) for component kkk
  • ∑k=1Kπk=1\sum_{k=1}^{K} \pi_k = 1∑k=1K​πk​=1 and πk≥0\pi_k \geq 0πk​≥0

Interpretation: Data is generated by first choosing a cluster kkk with probability πk\pi_kπk​, then sampling from N(μk,Σk)\mathcal{N}(\mu_k, \Sigma_k)N(μk​,Σk​).

FIG. 04Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive Python code execution environment

2. The EM Algorithm

Since we don't know which component generated each point, we use Expectation-Maximization (EM):

E-Step (Expectation)

Compute responsibility γik\gamma_{ik}γik​ = probability that point iii belongs to component kkk:

γik=πkN(xi∣μk,Σk)∑j=1KπjN(xi∣μj,Σj)\gamma_{ik} = \frac{\pi_k \mathcal{N}(x_i | \mu_k, \Sigma_k)}{\sum_{j=1}^{K} \pi_j \mathcal{N}(x_i | \mu_j, \Sigma_j)}γik​=∑j=1K​πj​N(xi​∣μj​,Σj​)πk​N(xi​∣μk​,Σk​)​

Interpretation: "How much" does component kkk explain point iii?

M-Step (Maximization)

Update parameters using responsibilities:

πk=1N∑i=1Nγik\pi_k = \frac{1}{N}\sum_{i=1}^{N} \gamma_{ik}πk​=N1​i=1∑N​γik​ μk=∑i=1Nγikxi∑i=1Nγik\mu_k = \frac{\sum_{i=1}^{N} \gamma_{ik} x_i}{\sum_{i=1}^{N} \gamma_{ik}}μk​=∑i=1N​γik​∑i=1N​γik​xi​​ Σk=∑i=1Nγik(xi−μk)(xi−μk)T∑i=1Nγik\Sigma_k = \frac{\sum_{i=1}^{N} \gamma_{ik}(x_i - \mu_k)(x_i - \mu_k)^T}{\sum_{i=1}^{N} \gamma_{ik}}Σk​=∑i=1N​γik​∑i=1N​γik​(xi​−μk​)(xi​−μk​)T​

Algorithm Steps

  1. Initialize: Random μk\mu_kμk​, Σk\Sigma_kΣk​, πk\pi_kπk​
  2. E-step: Compute responsibilities γik\gamma_{ik}γik​
  3. M-step: Update parameters using responsibilities
  4. Repeat until convergence (log-likelihood stops improving)
FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

3. GMM vs K-Means

Comparison

AspectK-MeansGMM
AssignmentHard (0 or 1)Soft (probabilities)
Cluster shapeSphericalElliptical (any orientation)
AlgorithmLloyd's algorithmEM algorithm
OutputCluster labelsProbabilities + density model
GenerativeNoYes (can sample new data)
Anomaly detectionDifficultNatural (low likelihood)
SpeedFasterSlower

When to Use Each

FIG. 08Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive Python code execution environment

4. Anomaly Detection with GMM

GMMs naturally support anomaly detection: points with low likelihood under the model are anomalies!

Anomaly Score(x)=−log⁡p(x)\text{Anomaly Score}(x) = -\log p(x)Anomaly Score(x)=−logp(x)
FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

5. Model Selection: Choosing K

How many components should we use? Use information criteria:

Bayesian Information Criterion (BIC)

BIC=−2log⁡L+klog⁡n\text{BIC} = -2 \log \mathcal{L} + k \log nBIC=−2logL+klogn

Where:

  • L\mathcal{L}L = likelihood
  • kkk = number of parameters
  • nnn = number of samples

Lower BIC = better model. BIC penalizes model complexity.

FIG. 12Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive Python code execution environment

Key Takeaways

✅ GMMs model data as a mixture of Gaussian distributions

✅ Soft clustering: Points can belong to multiple clusters with probabilities

✅ EM algorithm: Iteratively estimates responsibilities (E-step) and updates parameters (M-step)

✅ Advantages over K-Means: Elliptical clusters, probabilistic assignments, generative model

✅ Anomaly detection: Natural with likelihood-based scoring

✅ Model selection: Use BIC or AIC to choose number of components


What's Next?

Next lesson: Neural Networks Fundamentals – from biological inspiration to backpropagation!


Further Reading

Interactive Visualizations

  • scikit-learn — GMM Covariance Comparison — see how full, tied, diag, and spherical covariance matrices change cluster shape on the same data.
  • scikit-learn — Density Estimation: GMM — runnable example with concentric distributions and overlapping components.
  • Distill — Gaussian Process — neighboring topic with the same Gaussian-distribution intuition you're using here.

Video Tutorials

  • StatQuest — Gaussian Mixture Models, Clearly Explained (Josh Starmer).
  • StatQuest — Expectation-Maximization — the underlying algorithm, broken down.
  • Mathematicalmonk — EM Algorithm Series — the most rigorous free walkthrough.

Papers & Articles

  • Maximum Likelihood from Incomplete Data via the EM Algorithm — Dempster, Laird, Rubin, JRSS 1977. The foundational EM paper.
  • A View of the EM Algorithm that Justifies Incremental, Sparse, and Other Variants — Neal & Hinton, 1998. Why EM works and when it doesn't.
  • Variational Inference: A Review for Statisticians — Blei, Kucukelbir, McAuliffe, JASA 2017. The modern probabilistic-modeling generalization of EM.

Documentation & Books

  • scikit-learn: Gaussian Mixture Models — covers GaussianMixture and BayesianGaussianMixture (which auto-selects K).
  • Book: Pattern Recognition and Machine Learning — Bishop (Chapter 9). Still the gold standard for GMMs and EM.
  • Book: Bayesian Reasoning and Machine Learning — Barber (free PDF).
СВЯЗАННЫЕ ПОНЯТИЯ
gmmem-algorithmprobabilistic-clustering
← НАЗАД
03. Anomaly Detection: Isolation Forest & One-Class SVM
ДАЛЕЕ →
05. Neural Networks Fundamentals: Perceptrons to Backpropagation
ПРИБОРЫ НА СТРАНИЦЕ · 01
FIG. 02 · INTERACTIVE
Python Code Executor
ВАШИ ЗАМЕТКИ