S14 · Lesson 51 · Clustering project
Fitting a Gaussian mixture by expectation-maximization and watching its log-likelihood climb, computing principal components and checking that reconstruction error equals the discarded eigenvalues, then combining the two to find clusters in ten-dimensional data without labels
Unlabeled data is the common case in practice: logged sensor readings, test measurements from a production line, signals from an unknown number of sources. This project builds a small unsupervised pipeline from lessons 36 and 40. You'll write EM for a one-dimensional Gaussian mixture and confirm its key guarantee, a log-likelihood that never decreases. You'll write PCA and confirm an exact identity relating reconstruction error to eigenvalues. Then you'll use PCA to find the one direction that matters in ten-dimensional data and EM to separate the two populations along it.
Predict
Two clusters in 10 dimensions have centers 6 units apart along one direction and unit-variance noise in every direction. What's the largest eigenvalue of the data's covariance?
Along the line joining the centers, each point sits at ±3 plus unit noise, so the variance there is about 3² + 1 = 10 (a little less with unequal cluster sizes: 0.4 × 0.6 × 6² + 1 = 9.6). Every perpendicular direction has only the noise variance, 1. PCA finds that one direction immediately, and in the test data its eigenvalue comes out at 9.8.
Name it
Step 1: EM
em_gmm_1d alternates lesson 36's E step, , and M step, , , . Record before each update; the tests check it never decreases and that 2000 samples from 0.3 N(−2, 0.5²) + 0.7 N(1.5, 1) give back those parameters.
Step 2: PCA
pca returns the mean, the top M eigenvectors of the covariance (with 1/N normalization) as rows, and all eigenvalues, largest first. reconstruct maps each point onto the components and back. The tests build data whose covariance is exactly lesson 40's [[3, 1], [1, 2]], and check that the mean squared reconstruction error equals the sum of the discarded eigenvalues.
Step 3: clustering
cluster projects ten-dimensional data onto its first principal component and fits a two-component mixture there, starting the means at the 25th and 75th percentiles so the components start on opposite sides. Each point gets the label of the component with the larger responsibility.
Worked example
What does one EM step give for the points 0, 1 and 4, starting from means 0 and 4, unit variances and equal weights?
Show the solution
- E step. The first component takes responsibilities 0.99966, 0.98201 and 0.00034, so
- First component's M step. Its new mean is 0.496 and its variance 0.252.
- Second component's M step. It moves to mean 3.946 with variance 0.161.
- Log-likelihood. Before the step it is −5.317.
Answer after one step = 0.661, the means are 0.496 and 3.946, and the variances 0.252 and 0.161; the log-likelihood before the step is −5.317.
Worked example
What should the finished pipeline report on the test data?
Show the solution
- Step 1's fit. Means −1.983 and 1.459, widths 0.493 and 1.026 and weights 0.305 and 0.695, close to the generating values.
- Log-likelihood. It climbs from −4174 to −3605 and never falls.
- Step 3's components. The first eigenvalue is 9.82 against about 1.2 for the next, and the first component lines up with the true direction to within 4° (cosine 0.998).
- Labels. They match the truth for all 400 points.
Answer means −1.983 and 1.459, widths 0.493 and 1.026, weights 0.305 and 0.695, a log-likelihood that climbs from −4174 to −3605, a first eigenvalue of 9.82 with its component within 4° of the true direction, and correct labels for all 400 points.
Check
Step 1. Write em_gmm_1d: expectation-maximization for a Gaussian mixture.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write pca and reconstruct: principal components from the covariance.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write cluster: find two populations in ten dimensions without labels.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why reduce the data with PCA before clustering, instead of fitting a mixture in all ten dimensions?
A full-covariance Gaussian mixture in ten dimensions needs ten means and 55 covariance entries per component, and with a few hundred points those estimates are noisy and EM can wander into poor local maxima. Here the two populations differ along a single direction, and that direction carries far more variance than any other, so the first principal component captures nearly all the information that separates them. Projecting onto it reduces the problem to fitting five numbers in one dimension, where EM is fast and reliable. The risk is that PCA keeps directions of high variance, which aren't always the directions that separate clusters, so it works when the cluster separation dominates the spread, as it does here.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.