Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 47

K-Means Algorithm: Derivation and Convergence Proof

Recommended resources

Hello! Welcome to the next lesson in our journey through Data Science.

In our last session, we delved into the world of supervised classification by deriving the logistic regression model from first principles. We saw how to model a binary outcome by transforming a linear model's output into a probability.

Today, we make a pivotal shift from supervised to unsupervised learning. What if our data has no labels? How can we discover inherent structures or groups within it? This is the fundamental question of clustering.

This lesson introduces one of the most foundational and widely-used clustering algorithms: K-means. In line with your interest in the mathematical foundations of these methods, our objective is to derive the update steps for the K-means algorithm and prove its convergence to a local minimum. We will define the problem as an optimization task, derive the solution, and then mathematically guarantee that the algorithm will indeed find a solution.

1. The K-means Objective Function

The goal of K-means is to partition a set of data points into distinct clusters. The core idea is to find clusters where the data points within each cluster are as similar as possible. In K-means, "similarity" is defined by spatial proximity. We want to minimize the intra-cluster variance, which is equivalent to minimizing the sum of squared Euclidean distances from each data point to the center of its assigned cluster.

Let's formalize this. We need to find:

  1. A set of cluster centroids (or means), .
  2. An assignment of each data point to one of these clusters.

We can define an objective function, often denoted , which we aim to minimize. This function is the sum of the squared distances between each data point and the centroid of the cluster it's assigned to.

Concept — Probability & Statistics - GitHub Pages

To begin, let's formally define the K-means problem and its objective function. This resource lays out the mathematical notation we will use throughout the lesson.

Please read the subsections 'Cost Function' and 'Objective Function'. Pay close attention to the formulation in Equation (124) and (125). Notice how the total cost is a sum over all points of the squared distance to their assigned centroid.

As defined in the reading, our objective is to minimize:

where is the set of points assigned to cluster , and is the centroid of that cluster. We want to find the cluster assignments and centroid locations that make this total "cost" as small as possible.


2. Deriving the K-means Algorithm

Finding the global minimum of is an NP-hard problem. This means we can't solve it directly for any reasonably sized dataset. Instead, K-means uses an iterative, greedy approach often called alternating optimization or coordinate descent. We don't optimize for cluster assignments and centroids simultaneously. Instead, we alternate between two steps:

  1. Assignment Step: Assume the centroids are fixed, and find the optimal assignment for each data point.
  2. Update Step: Assume the assignments are fixed, and find the new optimal locations for the centroids.

Let's derive the rules for each step.

Derivation of the Assignment Step

Assume we have a fixed set of centroids from the previous iteration. We need to assign each data point to a cluster. Our goal is to minimize the total cost .

The total cost is a sum of individual costs for each point: . To minimize this sum, we can simply minimize each term independently. For a single data point , its contribution to the cost is minimized by assigning it to the cluster whose centroid is the closest.

This gives us our first rule:
Assignment Rule: Assign each data point to the cluster corresponding to the nearest centroid.

Here, is the cluster index assigned to point .

Derivation of the Update Step

Now, assume we have fixed cluster assignments from the previous step. For each cluster , we have a set of points . We need to find the new location for the centroid that minimizes the sum of squared distances for just that cluster.

The cost for a single cluster is:

This is a quadratic function of and is therefore convex. We can find the minimum by taking the gradient with respect to and setting it to zero. Your background in mechanics and vector calculus will be useful here.

Let's expand the squared norm: .

The gradient with respect to the vector is:

Using the standard identities and , we get:

Setting the gradient to zero to find the minimum:


Let be the number of points in cluster .

This elegant result gives us our second rule:
Update Rule: The optimal centroid for a cluster is the mean (or center of mass) of all the data points assigned to it.

The following resource provides a formal write-up of these two derivations.

Concept — Probability & Statistics - GitHub Pages

This reading formally proves the two necessary conditions for minimizing the K-means objective function, which correspond to the two steps we just derived.

Read the sections 'The Necessary Conditions to Minimize the Objective Function' and 'Algorithm'. Focus on 'Condition 1' for the assignment rule. Focus on 'Condition 2' for the centroid update rule, and follow the calculus which matches our derivation above. Finally, look at the 'Algorithm' section to see how these two steps are put together into an iterative process.

Visually, this iterative process of assigning points and updating centroids looks like this:

K-means Clustering Iterations
This image illustrates the K-means algorithm in action. (a) Initial random centroids. (b) Points are assigned to the nearest centroid, forming initial clusters. (c) Centroids are moved to the mean of their new clusters. This process repeats until, as shown in (d), the centroids and cluster assignments no longer change, indicating convergence.

3. Proof of Convergence

We've derived the steps of the algorithm, but how do we know it will ever stop? We need to prove that it converges. The proof is surprisingly straightforward and relies on two key facts.

  1. The objective function is monotonically non-increasing at every step.
  2. The number of possible partitions of the data is finite.

Let's watch a video that walks through the argument for convergence in detail.

Convergence of K-means algorithm

This video from IIT Madras provides a clear, step-by-step argument for the convergence of the K-means algorithm. It formalizes the intuition that the cost must go down with each step.

Watch the entire video (up to 33:11). The presenter does an excellent job of breaking down the proof: Fact 1 (03:36): Establishes that the mean minimizes the sum of squared distances, which is the core of our Update Step derivation. The Main Argument (07:05): Shows how the objective function at iteration t+1 is strictly less than at iteration t if any reassignment happens. The Finiteness Argument (27:16): Explains why a monotonically decreasing objective function combined with a finite number of possible clusterings guarantees convergence.

Let's summarize the proof formally. Let be the value of the objective function at the beginning of iteration . The centroids are and the cluster assignments are .

  1. Assignment Step: We re-assign points to their closest centroid in to get new clusters . By the definition of this step (assigning each point to its closest centroid), the sum of squared distances cannot increase. If any point moves to a new cluster, it must be because its new centroid is strictly closer, so the cost will strictly decrease.

  2. Update Step: We compute the new centroids by taking the mean of the points in the new clusters . As we proved in our derivation of the update step (and as shown in the video's "Fact 1"), the mean is the point that minimizes the sum of squared distances for a set of points. Therefore, this step also cannot increase the cost.

Combining these two inequalities, we get:

This shows that the objective function is monotonically non-increasing.

Since the objective function can never be negative, it is bounded below by 0. Furthermore, there is a finite number of ways to partition data points into clusters (the number is at most ). Since the cost function strictly decreases whenever the cluster assignments change, the algorithm can never repeat a partition. A sequence that is monotonically decreasing and bounded below, and can only take on a finite number of values, must converge.

Therefore, the K-means algorithm is guaranteed to converge to a local minimum in a finite number of steps. It's important to note that it does not guarantee finding the global minimum; the final result depends on the initial placement of centroids.


Conclusion

In this lesson, we have rigorously analyzed the K-means algorithm. We treated it not just as a set of instructions, but as an optimization procedure for a clearly defined objective function.

Key Takeaways:

  • K-means aims to minimize the sum of squared Euclidean distances from each point to its assigned cluster centroid.
  • The algorithm proceeds via alternating optimization:
    1. Assignment Step: Each point is assigned to its closest centroid.
    2. Update Step: Each centroid is moved to the mean of the points assigned to it.
  • Both of these steps were derived by minimizing the objective function while holding the other variable fixed.
  • Convergence is guaranteed because the objective function monotonically decreases at each step, and there is a finite number of possible cluster configurations. The algorithm stops when cluster assignments no longer change, which corresponds to a local minimum of the cost function.


Preview of the Next Lesson

We've established how K-means works and that it converges. However, K-means makes a "hard" assignment—each point belongs to exactly one cluster. What if we want to model "soft" assignments, where a point might belong to multiple clusters with varying probabilities? In our next lesson, we will explore the Gaussian Mixture Model (GMM), which provides this probabilistic perspective on clustering. We will derive its Expectation-Maximization (E-M) algorithm and compare its assumptions and use cases to those of K-means. We will also explore the beautiful geometric interpretation of K-means as creating a Voronoi tessellation of the data space.