Key Moments

Stanford CS229 Machine Learning | Spring 2026 | Lecture 9: K-Means and GMM (non-EM)

Stanford OnlineStanford Online
Education5 min read77 min video
Jul 31, 2026|472 views|7
Save to Pod

Want to know something specific about what's covered?

We've already dissected every moment. Ask and we will deliver (with timestamps).

TL;DR

K-means and Gaussian Mixture Models cluster data by iteratively refining cluster centers, but K-means' NP-hard problem means it may not find the optimal solution.

Key Insights

1

K-means is an iterative algorithm that aims to partition data into K clusters by minimizing the distance between data points and their assigned cluster centroid.

2

The K-means clustering problem is NP-hard, meaning finding the globally optimal solution is computationally challenging, and the algorithm may converge to a local optimum.

3

K-means++ initialization is a common default in libraries like scikit-learn, which improves the chances of finding a good clustering solution by a provable approximation ratio.

4

Gaussian Mixture Models (GMMs) extend K-means by using a probabilistic approach with Gaussian distributions, allowing for soft assignments of data points to clusters.

5

The Expectation-Maximization (EM) algorithm is used to train GMMs, involving an E-step to estimate cluster probabilities and an M-step to update model parameters (means, covariances, and mixing coefficients).

6

The choice of K (number of clusters) in both K-means and GMMs is a modeling decision that requires domain knowledge or further analysis, as simply increasing K does not guarantee better results.

The shift from supervised to unsupervised learning

The lecture begins by contrasting supervised learning, where labeled data is used to draw boundaries (e.g., classifying positive and negative points), with unsupervised learning. In unsupervised learning, labels are absent, and the goal is to discover inherent structure within the data, such as clusters. This shift requires different assumptions and often leads to weaker guarantees compared to supervised methods. The challenge lies in defining what constitutes a 'good' model or cluster without explicit labels, necessitating stronger prior assumptions about the data generation process.

K-means: An intuitive clustering algorithm

K-means is introduced as a popular unsupervised learning algorithm for clustering. The objective is to partition N data points into K clusters. The algorithm works iteratively: it starts with K initial cluster centroids, assigns each data point to the nearest centroid, and then recalculates the centroid as the mean of all points assigned to that cluster. This process repeats until the cluster assignments stabilize or a maximum number of iterations is reached. The core idea is to minimize the within-cluster sum of squares (WCSS), also known as inertia. While intuitive, K-means has limitations. The problem of finding the globally optimal set of clusters is NP-hard, meaning the algorithm can converge to a local optimum depending on the initial centroid placement. The lecture also touches upon the K-means++ initialization strategy, which aims to improve the quality of the initial centroids and has been shown to provide a good approximation ratio.

Challenges and considerations in K-means

Several critical questions arise when using K-means. Firstly, the algorithm's convergence is guaranteed because the objective function (WCSS) decreases monotonically with each iteration. However, it may converge to a local minimum, not the global one. The choice of initial centroids is crucial, and running the algorithm multiple times with different initializations is a common practice. Secondly, the choice of K, the number of clusters, is a significant modeling decision. There's no single objective metric that definitively tells you the 'correct' K. Visualizations like the 'elbow method' are often used, but they can be subjective. The lecture emphasizes that unsupervised learning methods like K-means are most meaningful when there is an underlying structure in the data that the algorithm can discover; applying it to random, unstructured data will yield meaningless clusters. The lecture also briefly mentions that in high-dimensional spaces, the concept of distance and clustering can behave in counter-intuitive ways.

Gaussian Mixture Models (GMMs): A probabilistic approach

Gaussian Mixture Models (GMMs) offer a more flexible and probabilistic alternative to K-means. Instead of assigning each data point strictly to one cluster, GMMs assume that the data is generated from a mixture of several Gaussian distributions, each representing a cluster. Each data point has a probability of belonging to each of these Gaussian components. This 'soft' assignment allows GMMs to model clusters with different shapes (ellipsoidal, not just spherical) and sizes, which K-means cannot easily do. The parameters of each Gaussian component (mean, covariance) and the mixing coefficients (which represent the probability of a data point coming from a particular component) are learned from the data.

The Expectation-Maximization (EM) algorithm for GMMs

The parameters of a GMM are typically estimated using the Expectation-Maximization (EM) algorithm. EM is an iterative procedure that alternates between two steps: the Expectation (E) step and the Maximization (M) step. In the E-step, given the current model parameters, the algorithm estimates the probability that each data point belongs to each Gaussian component (these are often called 'responsibilities'). In the M-step, given these estimated probabilities, the algorithm updates the model parameters (means, covariances, and mixing coefficients) to maximize the likelihood of the observed data. This process is repeated until convergence, where the parameters no longer change significantly. The EM algorithm is a general framework for fitting models with latent variables.

Jensen's inequality and convex optimization in EM

The lecture delves into the mathematical underpinnings of EM, introducing Jensen's inequality and the concept of convexity. Jensen's inequality is crucial for understanding why the EM algorithm converges and for deriving the update rules. It relates the value of a convex function of a random variable to the random variable's expected value. For concave functions (like the logarithm of probabilities, which is often optimized in machine learning), the inequality is reversed. The EM algorithm essentially optimizes a lower bound on the log-likelihood of the data. The M-step involves maximizing this lower bound, and the E-step calculates the expected value of the latent variables given the current parameters, which is then used to update the parameters in the next M-step. This process iteratively improves the model's fit to the data.

Model selection and the role of K

A recurring theme is the challenge of selecting the number of components, K, for both K-means and GMMs. This is a modeling choice that often relies on domain knowledge or techniques like the elbow method or Bayesian Information Criterion (BIC) / Akaike Information Criterion (AIC). The lecture highlights that simply increasing K will lead to a better fit to the training data (lower error/higher likelihood), but this can result in overfitting. The goal is to find a K that captures the underlying structure without being overly complex or sensitive to noise. The choice of K is not just a technical parameter but reflects assumptions about the generative process of the data.

Common Questions

Unsupervised learning deals with data that has no labels or classifications, unlike supervised learning where data is labeled. The goal in unsupervised learning is to find inherent structures, patterns, or clusters within the data itself.

Topics

Mentioned in this video

More from Stanford Online

View all 106 summaries

Ask anything from this episode.

Save it, chat with it, and connect it to Claude or ChatGPT. Get cited answers from the actual content — and build your own knowledge base of every podcast and video you care about.

Get Started Free