◉ Yashvardhan Guptaall writing ↗

Essay

From K-Means to Gaussian Mixture Models: The Math, The Intuition, and Everything In Between

Why your favorite clustering algorithm is secretly a special case of something much more powerful — a journey through K-Means, GMMs, MLE, and the EM algorithm.

February 10, 2026 · Yashvardhan Gupta

Why your favorite clustering algorithm is secretly a special case of something much more powerful.


If you've ever used K-Means clustering, you've already understood 70% of Gaussian Mixture Models. You just don't know it yet.

In this post, I'm going to take you on a journey — starting from the familiar territory of K-Means, revealing its hidden limitations, and showing you how Gaussian Mixture Models (GMMs), Maximum Likelihood Estimation (MLE), and the Expectation-Maximization (EM) algorithm form one beautifully connected story. Not four separate topics. One story.

Here's the narrative arc:

K-Means → its limitations → GMM as the upgrade → MLE as the fitting principle → EM as the algorithm that makes it all work.

Let's get into it.


K-Means: What Are You Actually Optimizing?

Most people learn K-Means as a recipe. Pick K centroids randomly. Assign each data point to its nearest centroid. Recompute centroids as the mean of assigned points. Repeat until convergence. Done.

But here's a question that surprisingly few people ask: what objective function is K-Means minimizing?

Think about it — at each step, you want points to be close to their assigned centroids. If you had to write a single number that captures "how good is this clustering," you'd naturally want something that's small when points hug their centroids and large when they're scattered. The most natural choice? Squared distances.

And that's exactly it. K-Means minimizes the Within-Cluster Sum of Squares (WCSS):

\[J = \sum_{k=1}^{K} \sum_{x_i \in C_k} \|x_i - \mu_k\|^2\]

Here, \(C_k\) is the set of points assigned to cluster \(k\), and \(\mu_k\) is the centroid of that cluster. The assignment step minimizes \(J\) with respect to cluster memberships (each point picks its closest centroid), and the update step minimizes \(J\) with respect to centroid positions (the mean minimizes sum of squared distances). That's why K-Means is guaranteed to converge — \(J\) decreases or stays the same at every step.

Simple. Elegant. But stare at that objective for a second and notice what it doesn't capture.


The Two Blind Spots of K-Means

Blind spot #1: Cluster shape. K-Means uses Euclidean distance, so it always carves out roughly spherical regions around each centroid. What if your cluster is elongated like an ellipse? Or tilted at an angle? K-Means can't see that. It treats every cluster as a perfect circle.

Blind spot #2: Uncertainty. Every point gets a hard assignment — it belongs to exactly one cluster. But what about a point sitting right on the boundary between two clusters? K-Means forces a binary choice. There's no room for "this point is 70% likely to be in cluster 1 and 30% likely to be in cluster 2."

So the natural question becomes: can we upgrade K-Means to handle both shape and uncertainty?

What if, instead of a bare centroid at each cluster center, we placed a full Gaussian distribution — something that has a mean \(\mu_k\) (location, like the centroid) and a covariance matrix \(\Sigma_k\) (shape — circular, stretched, tilted, whatever the data demands)? And instead of hard assignment, we got soft probabilities for each point?

That's exactly what a Gaussian Mixture Model does.


Gaussian Mixture Models: The Probabilistic Upgrade

A GMM is a probabilistic model that tells a story about how your data was generated. Now, before you raise an eyebrow — we're not literally generating anything. We already have the data. The "generative story" is our hypothesis about the data's structure. It's like a detective arriving at a crime scene: the evidence is already there, but to make sense of it, you construct a theory of how the crime happened. Then you ask: does this theory explain the evidence well?

Here's the GMM's theory of how your data came to be:

For each data point \(x_i\), nature did two things:

Step 1: It rolled a K-sided weighted die. The weight on side \(k\) is \(\pi_k\), called the mixing coefficient. All the \(\pi_k\)'s are non-negative and sum to 1 — they form a probability distribution over clusters. Think of \(\pi_k\) as the answer to: "if I pick a random data point, what's the probability it came from cluster \(k\)?"

Step 2: Having chosen cluster \(k\), nature drew \(x_i\) from the Gaussian \(\mathcal{N}(\mu_k, \Sigma_k)\).

Step 3 (the tricky part): Nature then hid which cluster it chose. You only see \(x_i\) — you don't know which Gaussian it came from.

That hidden choice is called the latent variable. It's the piece of information that, if you knew it, would make everything trivially easy. If someone told you "these 60 points came from Gaussian 1 and those 40 from Gaussian 2," you'd just do regular statistics on each group separately. But nobody tells you that. That's the whole challenge.

The Math: Probability of a Single Point

Since we don't know which Gaussian generated \(x_i\), we have to account for all possibilities. By the law of total probability:

\[p(x_i) = \sum_{k=1}^{K} \pi_k \cdot \mathcal{N}(x_i \mid \mu_k, \Sigma_k)\]

Each term in this sum says: "there's a \(\pi_k\) chance that \(x_i\) came from Gaussian \(k\), and if it did, the probability of seeing \(x_i\) is \(\mathcal{N}(x_i \mid \mu_k, \Sigma_k)\)." We sum over all clusters because we don't know which one was responsible.

The Parameters We Need to Learn

For each of the \(K\) Gaussians, we need to find:

So the full parameter set is \(\Theta = \{\pi_1, \mu_1, \Sigma_1, \ldots, \pi_K, \mu_K, \Sigma_K\}\).

The question is: how do we find the best \(\Theta\)? This is where MLE enters the picture.


MLE: The Fitting Principle

Maximum Likelihood Estimation is a beautifully simple idea: find the parameters that make your observed data most probable.

Let's warm up with a simple case first. Suppose you have data points \(x_1, x_2, \ldots, x_n\) and you believe they came from a single Gaussian — you just don't know which \(\mu\) and \(\sigma^2\). The likelihood of all your data (assuming independence) is:

\[L(\mu, \sigma^2) = \prod_{i=1}^{n} \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x_i - \mu)^2}{2\sigma^2}\right)\]

Products are ugly to work with, so we take the logarithm. Since \(\ln\) is a monotonically increasing function, the parameters that maximize \(L\) also maximize \(\ln L\):

\[\ell(\mu, \sigma^2) = -\frac{n}{2}\ln(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^{n}(x_i - \mu)^2\]

Take the derivative with respect to \(\mu\), set to zero, and you get \(\hat{\mu} = \frac{1}{n}\sum x_i\) — the sample mean. Take the derivative with respect to \(\sigma^2\), set to zero, and you get \(\hat{\sigma}^2 = \frac{1}{n}\sum(x_i - \hat{\mu})^2\) — the sample variance.

Clean. Closed-form. Beautiful.

Now let's try the same thing with a GMM.

MLE for GMMs: Where Things Break

The log-likelihood for our mixture model is:

\[\ell(\Theta) = \sum_{i=1}^{n} \ln \left( \sum_{k=1}^{K} \pi_k \cdot \mathcal{N}(x_i \mid \mu_k, \Sigma_k) \right)\]

Stare at this and compare it to the single-Gaussian case. In the single-Gaussian case, the log sits right next to a single exponential, and they cancel each other out. Everything simplifies into polynomial terms that are easy to differentiate.

But here? The log is outside a summation of exponentials. When you try to differentiate with respect to, say, \(\mu_1\), the derivative can't isolate \(\mu_1\) because it's entangled with terms from every other Gaussian through that sum inside the logarithm. You end up with expressions where \(\mu_1\) depends on quantities that themselves depend on \(\mu_2\), \(\Sigma_1\), \(\Sigma_2\), and all the \(\pi_k\)'s. Everything is coupled.

No clean closed-form solution exists.

So MLE is the right principle, but we can't solve it directly. Now what?


The EM Algorithm: A Clever Workaround

Here's the key insight: what made the GMM problem hard was the hidden information — we don't know which cluster generated each point. If someone whispered the cluster assignments to us, we'd just split the data into groups and do simple MLE on each group. Easy.

And the reverse is also true — if someone told us the exact parameters (\(\mu_k\), \(\Sigma_k\), \(\pi_k\)), computing how much each cluster "owns" each point is easy too. That's just Bayes' theorem.

So the Expectation-Maximization algorithm makes a clever play: alternate between these two easy steps, and hope that you converge to the right answer. (Spoiler: you do, provably.)

Bayes' Theorem in GMMs: The Responsibilities

Before we get to EM's two steps, let's set up the Bayesian machinery. For a data point \(x_i\) and cluster \(k\), Bayes' theorem gives:

\[P(\text{cluster}_k \mid x_i) = \frac{P(x_i \mid \text{cluster}_k) \cdot P(\text{cluster}_k)}{P(x_i)}\]

Let's map each term to the GMM world:

So the responsibility is:

\[\gamma_{ik} = \frac{\pi_k \cdot \mathcal{N}(x_i \mid \mu_k, \Sigma_k)}{\sum_{j=1}^{K} \pi_j \cdot \mathcal{N}(x_i \mid \mu_j, \Sigma_j)}\]

This tells you: "given the current parameters, what fraction of the credit for generating \(x_i\) should go to cluster \(k\)?" For a given point, the responsibilities across all clusters sum to 1. You might get something like \((0.7, 0.2, 0.1)\) — meaning 70% chance this point came from cluster 1, 20% from cluster 2, and 10% from cluster 3.

Compare this to K-Means, where the same point would get a hard assignment like \((1, 0, 0)\). That's the fundamental upgrade — soft vs. hard assignment.

Now we're ready for EM.


The E-Step (Expectation)

Given the current parameter estimates \(\Theta^{(t)}\), compute the responsibility of every cluster for every data point:

\[\gamma_{ik} = \frac{\pi_k \cdot \mathcal{N}(x_i \mid \mu_k, \Sigma_k)}{\sum_{j=1}^{K} \pi_j \cdot \mathcal{N}(x_i \mid \mu_j, \Sigma_j)}\]

That's it. Plug in your current guesses for \(\mu_k\), \(\Sigma_k\), \(\pi_k\) and evaluate Bayes' theorem for each point-cluster pair.

The M-Step (Maximization)

Given the responsibilities from the E-step, update all parameters using weighted MLE:

Updated mean: \[\mu_k = \frac{\sum_{i=1}^{n} \gamma_{ik} \, x_i}{\sum_{i=1}^{n} \gamma_{ik}}\]

Updated covariance: \[\Sigma_k = \frac{\sum_{i=1}^{n} \gamma_{ik} (x_i - \mu_k)(x_i - \mu_k)^T}{\sum_{i=1}^{n} \gamma_{ik}}\]

Updated mixing coefficient: \[\pi_k = \frac{1}{n} \sum_{i=1}^{n} \gamma_{ik}\]

Each of these is the weighted version of a formula you already know from basic statistics. The weights are the responsibilities.

A Concrete Example

Let's make this tangible. Suppose we have 5 data points: \(x = \{1, 2, 5, 6, 7\}\), and we're fitting \(K = 2\) Gaussians. After the E-step, say we computed these responsibilities:

Point \(x_i\) \(\gamma_{i1}\) (cluster 1) \(\gamma_{i2}\) (cluster 2)
1 0.9 0.1
2 0.8 0.2
5 0.3 0.7
6 0.1 0.9
7 0.1 0.9

Points 1 and 2 are strongly owned by cluster 1. Points 6 and 7 lean heavily toward cluster 2. Point 5 is somewhere in between but leans toward cluster 2.

Updating \(\mu_1\):

\[\mu_1 = \frac{(0.9)(1) + (0.8)(2) + (0.3)(5) + (0.1)(6) + (0.1)(7)}{0.9 + 0.8 + 0.3 + 0.1 + 0.1} = \frac{5.3}{2.2} \approx 2.41\]

Every point contributes, but points 1 and 2 dominate because their responsibility toward cluster 1 is high. In K-Means, only points hard-assigned to cluster 1 would contribute, giving you (1+2)/2 = 1.5. The GMM pulls \(\mu_1\) slightly rightward because point 5 has a 0.3 responsibility — a subtle influence K-Means would completely ignore.

Updating \(\sigma_1^2\):

\[\sigma_1^2 = \frac{(0.9)(1 - 2.41)^2 + (0.8)(2 - 2.41)^2 + (0.3)(5 - 2.41)^2 + (0.1)(6 - 2.41)^2 + (0.1)(7 - 2.41)^2}{2.2} \approx 3.34\]

Same principle — it's the weighted spread of data around \(\mu_1\). K-Means doesn't even compute this, because it assumes all clusters are spherical with equal variance.

Updating \(\pi_1\):

\[\pi_1 = \frac{0.9 + 0.8 + 0.3 + 0.1 + 0.1}{5} = \frac{2.2}{5} = 0.44\]

So \(\pi_2 = 0.56\). Cluster 2 is slightly "bigger" — it's responsible for 56% of the data. Makes sense: three out of five points lean toward cluster 2.

The intuition: each data point has a voice in defining every cluster, but the loudness of that voice is proportional to its responsibility. Point 1 shouts during cluster 1's update (\(\gamma = 0.9\)) but barely whispers during cluster 2's (\(\gamma = 0.1\)).

After this M-step, you go back to the E-step — recompute all responsibilities with these fresh parameters — and they shift slightly. Then M-step again. Back and forth until nothing changes.


K-Means Is a Special Case of EM

Now step back and see the full picture. K-Means and EM for GMMs have the exact same structure:

K-Means:

EM for GMMs:

K-Means is literally EM where you force the responsibilities to be 0 or 1 (hard assignment), constrain all clusters to have the same spherical covariance, and ignore the mixing coefficients. Relax those three restrictions, and you get the full EM algorithm for GMMs.


Why Does EM Actually Work?

Here's the deep question: why does this alternation converge to anything meaningful?

Remember, we couldn't maximize the log-likelihood directly because everything was coupled. EM sidesteps this by never solving the whole problem at once. The E-step says: "I'll pretend I know the parameters and just figure out the hidden assignments." The M-step says: "I'll pretend I know the assignments and just optimize the parameters."

The mathematical guarantee is this: each EM iteration never decreases the log-likelihood. The E-step computes a lower bound on the log-likelihood, and the M-step maximizes that lower bound. So you keep pushing the likelihood up (or at least holding it steady) until you converge.

It's like climbing a hill when you can only move in one direction at a time — first East-West, then North-South, then East-West again. Each move takes you higher. You'll reach a peak, though not necessarily the highest peak — EM converges to a local maximum, not a global one. That's why initialization matters. A common trick is to run K-Means first and use its output as the starting point for EM — getting a reasonable starting position before refining with the full probabilistic machinery.


The Complete Picture

Let's zoom all the way out and see how everything connects:

K-Means gave us an intuitive clustering algorithm, but with hard assignments and spherical clusters. We asked: can we do better?

GMMs answered yes — by modeling each cluster as a full Gaussian distribution, we get soft assignments (uncertainty) and flexible cluster shapes (covariance matrices). But fitting a GMM requires finding the parameters that best explain the data.

MLE is the principle that tells us what "best" means — find the parameters that make the observed data most probable. For a single Gaussian, MLE gives clean closed-form solutions. For a mixture of Gaussians, the log-of-a-sum structure makes direct optimization intractable.

EM is the algorithm that rescues us — by cleverly alternating between computing responsibilities (E-step) and updating parameters (M-step), it iteratively climbs the likelihood surface until convergence, all while keeping each individual step simple and closed-form.

Four topics. One story. Each one exists because of a limitation in the previous one.


If you found this useful, I write about the math behind machine learning — the kind that builds intuition, not just notation. Follow along if you want to go deep without getting lost.

← back home