Unit 5 of 8 · about 3 hours · derive
Gaussian mixtures and EM
Mixtures turn distance into probability: overlapping groups get graded membership plus density scores that the anomaly unit spends.
You'll be able to
- Explain responsibilities as posterior component membership and the EM loop as alternating and steps.
- Derive the 1D M-step updates for means, variances and weights from weighted maximum likelihood.
- Select the component count with BIC or AIC and state why a plain
GaussianMixturealways uses every component.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Mixture fits, covariances and component countHands-On Machine Learning with Scikit-Learn and PyTorch - dimensionality reduction and unsupervised learning
Book Chapter 8: Gaussian Mixtures through Selecting the Number of Clusters (printed pp. 269-278). Notebook 08 cells: Gaussian Mixtures: EM-fitted weights, means and covariances on the three-ellipsoid data with the
covariance_typefull,tied,sphericalanddiagcomparison; Selecting the Number of Clusters with the BIC and AIC formulas and the minimum atSkim Bayesian Gaussian Mixture Models; variational inference is later work. Keep the BIC and AIC minimum plot.
- EM estimation, init and failure modesscikit-learn - decomposition, clustering and outlier detection
2.1.1 Gaussian Mixture: EM E-step and M-step description, covariance options, the singularity and always-uses-all-components cons, and the BIC asymptotic note; Choice of the Initialization method with the k-means default
Focus on
- Responsibilities are posteriors that sum to one per point. The M-step refits each Gaussian to responsibility-weighted data.
- Likelihood rises every EM round but only to a local optimum. Starts matter; the sklearn default starts from k-means.
- Full covariance needs parameters per component. Few points per component invite singular covariances;
reg_covar(1e-6 by default) adds to the diagonal to keep them invertible. - BIC penalizes parameters with . It recovers the true count only asymptotically under a true mixture; on real data it is a guide, not proof.
Common mistakes · 3
- Dividing the M-step variance by . Mixture MLE divides by the responsibility sum, which is for , giving 2.6667 on , not 4.0.
- Reading converged likelihood as a global optimum. Different starts converge differently; report spread, not one run.
- Trusting the BIC minimum as truth on misspecified data. The docs note asymptotic recovery needs data truly drawn from a mixture.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.What does responsibility 0.9 of point under component 2 mean?Answer
Posterior membership: under current parameters the probability is 0.9 that component 2 generated . It is not a hard label.
2. EM on gives variance 2.6667. Derive it.Answer
Mean 4.0; squared deviations 4+0+4=8; MLE divides by 3. The unbiased 4.0 answers a different question.
3.Weights converge to on two overlapping groups. What do you conclude?Answer
One component explains nearly everything. Try , check BIC, and consider that the small component fits outliers.
4.Full covariance on 50 points in 10 dimensions with . What breaks?Answer
Each full covariance needs 55 parameters from about 10 effective points. Expect singularities; use
tiedordiag, regularize, or cut .
Practice
- Deep-ML 341: Gaussian Mixture Model with EM Algorithm
Problem 341: implement
fit_gmm_1dwith and steps forn_iterations, rounding means, variances and weights to 4 decimalsTest one checks the closed form with means , variances and weights ; test two checks a symmetric case stays at means with variances . Derive both by hand first.
You're done when
You derive the numbers by hand, pass both Deep-ML tests, and can predict how tied versus full covariance changes the parameter count.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 6 more in the section
- Hands-On Machine Learning with Scikit-Learn and PyTorch - dimensionality reduction and unsupervised learningAurelien Geron (O'Reilly, 2025) · primary
Read Chapters 7 and 8 and run notebooks 07 and 08 for PCA, k-means, DBSCAN, agglomerative clustering, Gaussian mixtures and anomaly detection. Stop before extended semi-supervised and face-data projects.
- scikit-learn - decomposition, clustering and outlier detectionscikit-learn · reference
Check decomposition orientation and solver behavior, cluster API semantics, and the distinction between novelty and outlier detection after the independent tests pass.
Also in this section
- ISLP - Unsupervised LearningJames, Witten, Hastie, Tibshirani and Taylor · required
Use Chapter 12 and its Python lab for the PCA derivations (12.2, including the low-rank approximation view and proportion of variance explained) and k-means (12.4.1), plus stability and interpretation limits, beside the notebook route.
- Deep-ML - unsupervised problemsDeep-ML · support
Use 17 K-Means Clustering, 19 PCA, 341 Gaussian Mixture Model with EM, 259 DBSCAN and 367 Isolation Forest as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin, including the 367 path-length convention.
- scikit-learn - preprocessing datascikit-learn · reference
Read 8.3.1 standardization with the sparse
with_mean=Falserule, 8.3.1.3 scaling data with outliers, and 8.3.4 categorical encoding withhandle_unknown, infrequent andsparse_outputbehavior. Apply after the HOML notebook build works. - scikit-learn - pipelines and composite estimatorsscikit-learn · reference
Read 8.1.1
Pipelinechaining with the no-leakage rule and transformer caching, and 8.1.4ColumnTransformerfor heterogeneous columns withremainderand fitted-state behavior. Use it to assemble and serialize the feature-selection pipeline. - scikit-learn - imputation of missing valuesscikit-learn · reference
Read 8.4.2 univariate median and
most_frequentstrategies, 8.4.6 Marking imputed values withMissingIndicatororadd_indicator=Truefor missingness flags, and 8.4.7 estimators that acceptNaNdirectly. Decide impute-only versus impute-plus-indicator per column. - UMAP - basic usage and parametersMcInnes, Healy and the UMAP contributors · reference
Work the Penguin scaling and
fit_transformsection and the Digitsfitandtransformsection, then read the parameters page for then_neighborslocal-global tradeoff andmin_distpacking. Stay in 2D exploratory views with fixed seeds.