Stats 401 - Week 7 - Part 1 - K-means and EM for Gaussian Mixtures
Overview
Miles Chen introduces unsupervised learning through K-means clustering and Gaussian mixture models fitted with expectation-maximization (EM), explaining how each method groups unlabeled observations and where its assumptions matter. He details K-means’ distance-based assignments, initialization sensitivity, scaling needs, and cluster-selection tools, then shows how EM uses probabilistic responsibilities and weighted updates to estimate Gaussian means, covariances, and mixing proportions; the lesson ends with BIC for choosing component count and a transition to PCA.
Key takeaways
- K-means alternates between nearest-centroid assignments and mean-based centroid updates, reducing within-cluster sum of squares but guaranteeing only a local solution.
- K-means’ Euclidean distances make feature scaling consequential; standardizing variables to mean 0 and standard deviation 1 can prevent large-range measurements from dominating.
- Gaussian mixture EM replaces hard labels with responsibilities computed by Bayes’ rule, then uses those weights to update component means, covariance matrices, and mixing proportions.
- Covariance-aware Gaussian mixtures can fit elongated clusters that distance-based K-means may divide poorly, and they express uncertain boundary cases as probabilities across components.
- Both K-means and EM depend on initialization, so multiple starts and selection criteria such as the elbow method, silhouette scores, or BIC help assess solutions and model complexity.
Chapters
- Miles Chen contrasts supervised learning, which uses target labels to evaluate predictions, with unsupervised learning, which searches for structure without targets.
- The week’s topics are K-means clustering, Gaussian mixtures with EM, and principal components analysis (PCA).
- Clustering groups similar observations, while PCA reduces dimensions while retaining much of the data’s variation.
- K-means groups observations by distance, commonly using Euclidean distance, and represents each cluster with its mean, or centroid.
- Other similarity measures include Manhattan distance and cosine similarity, but K-means uses Euclidean geometry.
- K-means is unsupervised and iterative, unlike supervised K-nearest neighbors, despite both using distances and a capital K.
- The indicator Zₙₖ equals 1 when observation n belongs to cluster k and 0 otherwise.
- A centroid is the sum of observations weighted by Zₙₖ, divided by the number of observations assigned to that cluster.
- The indicator formulation filters out observations outside the cluster when calculating its mean.
- Choose the number of clusters K, then initialize by randomly assigning observations or selecting starting centroids.
- Calculate each cluster’s mean, assign every observation to its nearest centroid, and repeat those updates.
- Stop when assignments stabilize, a maximum iteration count is reached, or centroid changes become sufficiently small.
- K-means minimizes within-cluster sum of squares, the total squared distance from each observation to its assigned centroid.
- Nearest-centroid assignment minimizes each observation’s contribution when centroids are fixed.
- For fixed cluster assignments, the mean minimizes the cluster’s squared-error total; alternating these steps cannot increase the objective.
- The elbow method compares within-cluster sum of squares across K values and looks for diminishing returns; silhouette scores range from -1 to 1.
- Standardization to Z-scores or min-max scaling to 0 through 1 prevents variables with larger numerical ranges from dominating Euclidean distances.
- K-means works best for compact, separated groups with straight-line or hyperplane boundaries and is sensitive to outliers.
- The example uses eight points in two compact groups: four near (0, 0) and four near (3.5, 3.5).
- Random initial assignments produce provisional centroids, after which nearest-centroid reassignment separates the two groups.
- The updated centroids are (0.5, 0.5) and (3.5, 3.5); no further reassignment occurs, so the algorithm converges.
- With assignments fixed, the cluster mean minimizes squared error; with centers fixed, assigning each point to its nearest center minimizes its contribution.
- These two updates make the within-cluster sum of squares decrease or stay constant until the assignments stabilize.
- A stable solution is optimal for its current labels and centers, but different initializations can lead to different local minima.
- Chen simulates 300 points from three groups centered at (0, 0), (-4.5, 0), and (4.5, 0), with substantial overlap.
- Random assignments lead to successive centroid and boundary updates that eventually form three clusters.
- The plotted within-cluster sum of squares declines with each update and levels off at convergence.
- Running K-means 20 times for each K and keeping the lowest within-cluster sum of squares helps address initialization sensitivity.
- For the simulated data, the best reported within-cluster sums of squares are 1356 for K=2, 616.89 for K=3, and 522.42 for K=4.
- Different K=4 runs can produce different groupings, and cluster color or number is arbitrary; membership is what matters.
- Using all 11 MTCars variables, the elbow plot suggests K=3 or K=4; Chen chooses K=3 and compares 20 initializations.
- R and Python agree on 31 of 32 cars, differing on a Porsche near a cluster boundary; the clusters broadly distinguish heavy, low-efficiency cars, compact efficient cars, and sporty cars.
- PCA projects the 11 variables to two dimensions for display, while silhouette scores computed on standardized variables flag the Porsche’s ambiguous membership.
- K-means assigns each point to exactly one cluster, while a Gaussian mixture model can assign probabilities across overlapping components.
- A boundary case such as the Porsche could be described with probabilities for heavy-car and sporty-car groups rather than a forced label.
- EM combines an iterative fitting process with Bayes’ rule, similar to the probabilistic reasoning used in a Bayes classifier.
- When a normal distribution’s mean is known to be 1, a missing observation can be estimated as 1; when all observations are known, their average estimates an unknown mean.
- With observed values 1 and 2 plus one missing value and an unknown mean, alternating estimates from an initial missing value of 0 converges to 1.5.
- EM generalizes this alternation: the E-step calculates an expected complete-data log-likelihood, and the M-step maximizes it.
- A simulated dataset contains 1,000 unlabeled observations from three components with mixing proportions 50%, 30%, and 20%.
- The components are centered at (0, 0), (4, 4), and (-4, -4), with distinct variances and positive or negative covariance patterns.
- Unlike Euclidean K-means, Gaussian mixtures can account for elongated elliptical shapes and assign a point probabilistically when it could plausibly come from multiple components.
- Each Gaussian component has its own mean and covariance matrix, while its mixing proportion α contributes to the probability of generating an observation.
- Because component labels are unknown, the likelihood sums over all components rather than treating memberships as observed.
- The E-step uses Bayes’ rule to calculate responsibilities, or membership weights, which sum to 1 across components for each observation.
- The effective size of component k is the sum of its responsibilities across observations.
- The M-step uses those weights to calculate a weighted mean and covariance matrix for each Gaussian component.
- Each updated mixing proportion is the component’s effective size divided by the total number of observations; log densities help avoid numerical underflow.
- EM alternates between responsibility updates using current means, covariances, and mixing proportions, and parameter updates using weighted observations.
- In the demonstration, initial component locations and covariance estimates are rough; repeated updates adjust both memberships and elliptical shapes.
- The fitted model converges toward the three simulated Gaussian groups, while plotted colors show only the most likely component and hide probabilistic uncertainty.
- Different starting means and covariances can converge to different local solutions, so multiple initializations are advisable.
- In the illustrated run, two starts reach a log-likelihood of -4703.57 after 38 and 41 iterations, with likelihood improving rapidly before leveling off.
- An observation near (2.16, 1.59) has roughly split membership across two components, while points near component centers can have probabilities near 99%.
- K-means uses nearest-centroid distance and straight boundaries, whereas Gaussian mixtures use covariance shape and probabilistic membership to handle elongated groups.
- Adding Gaussian components generally improves training likelihood, so maximum likelihood alone can favor an overly flexible model.
- The Bayesian information criterion (BIC) balances fit and model complexity; smaller BIC values are preferred, and excess components can raise the criterion.
- Chen pauses after covering K-means, Gaussian-mixture EM, and BIC-based component selection.
- PCA is announced as the next topic, building on its earlier introduction as a method for reducing correlated variables to fewer dimensions.
Transcript excerpt
0:03 Hello and welcome and thank you for tuning in to watch this video. And so this week I wanted to cover a few topics um that fit under the umbrella of unsupervised learning. So that's going to include uh clustering algorithms uh mixture um identifying mixtures with the EM algorithm and principal components analysis. So um so yeah let's get started and we'll um we'll talk about these different things. So um we've most of our things so far have been um supervised learning. But um but yeah this week uh I want to cover K-means
0:52 clustering which is going to take observations and group them together based on uh their distances uh which is how we're going to measure similarity or how different they are. We also have uh Gaussian mixtures and the EM the expectation maximization algorithm and that's going to um allow us to figure out uh probabilities of group membership. So it's similar to the clustering algorithm but it will allow for um yeah groups that overlap and uh and different kind of variances. And then um PCA principal components analysis
1:41 will allow us to take correlated variables and kind of decorrelate them and and um and it's often used in the case of dimension reduction which really allows us to use kind of um um fewer variables uh that uh that still preserves a lot of the variation that exists uh in the uh original data set. Okay? So yes, unsupervised learning is in contrast to supervised learning in that um when we had supervised learning, all of the values in our training set um came with target values. Okay? So
2:31 um you you know, whether it was generalized additive models um where you have these input variables X and you're trying to predict an output a target value. Okay?
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Miles Chen.