Derive the K-Means Objective and Its Assignment–Update Steps
Fit K-means twice on the same data with the same k, and you can get two different clusterings. The second run often reports a lower cost than the first.…

Key topics
Fit K-means twice on the same data with the same k, and you can get two different clusterings. The second run often reports a lower cost than the first. That single observation contains the whole theory: there is an objective, the algorithm only ever pushes it down, and the surface it descends has many valleys.
This article builds the formal spine. We will fix the notation, write the objective, derive both update steps as exact minimizers, and watch the objective fall in a hand-computed example. Then we will be precise about what convergence actually guarantees — and what it does not.
If you have already read the intuition piece on grouping data by proximity, treat scaling, choosing k, and interpreting clusters as given. Here we work on the objective itself.
What K-Means Is Actually Minimizing
Start with the data. We have points in dimensions, written as a matrix whose rows are the vectors . The number of clusters is fixed in advance.
A clustering is a partition of the points into sets . Disjoint, and covering every point: each point belongs to exactly one cluster.
The parameters the algorithm is allowed to move are the centroids , each a vector in .
The K-means objective function — the within-cluster sum of squares, or WCSS — is:
There is a second way to write the same quantity that makes the mechanism obvious:
Read that literally: every point pays for its distance to its nearest center, squared. The two forms are equal because each point's contribution depends only on the cluster it was assigned to, and the best assignment is always the nearest center.
Why squared Euclidean distance and not plain distance? Because the square is what makes the centroid update a closed-form mean. Swap in absolute distance and you get k-medians — a different objective with a different update rule. The choice of distance is the choice of algorithm.
Knowledge check
Check your understanding
Answer this question before you continue.
Two Blocks, One Objective: Why the Problem Is Hard
Look at again. It depends on two coupled sets of unknowns: the partition and the centroids. You cannot optimize both at once in closed form.
But each one alone is easy. If the centroids were known, the best assignment is trivial — send each point to its nearest center. If the assignment were known, the best centroids are trivial — average the points in each cluster. Neither is known.
Brute force is not an option. The number of ways to partition points into groups grows on the scale of Stirling numbers, which explodes past toy sizes almost immediately.
So we use the standard strategy for coupled unknowns: hold one block fixed, minimize exactly over the other, repeat. This is alternating minimization, also called block coordinate descent. It is the same shape as the E and M steps in expectation-maximization.
One assumption to state plainly: is non-convex jointly. That fact is exactly why the guarantee we eventually prove is local, not global.
Deriving the Assignment Step
Hold the centroids fixed. Now becomes a sum of independent per-point terms, because each point's contribution depends only on which centroid it joins. No term couples two points.
For a single point , its cost is for whichever it joins. Minimizing over means choosing the nearest centroid:
Squared distance and plain distance give the same argmin, so the square is harmless here — it only matters for the update step.
Geometrically, this step partitions space into Voronoi cells around the current centroids. Every point inside a cell is closer to that cell's center than to any other.
Note: When a point is equidistant from two centroids, the rule is ambiguous. Any consistent tie-break works; the objective value is unaffected. This is why two implementations can disagree on boundary points without either being wrong.
Why this step can only lower : you are replacing each point's current cost with the minimum over all centroids, and a minimum is never larger than the value you started with.
Knowledge check
Check your understanding
Answer this question before you continue.
Deriving the Centroid Update
Now hold the partition fixed. decomposes into independent subproblems, one per cluster, because no term couples two clusters. For cluster , minimize:
Take the gradient with respect to . Each term contributes , so:
Set the gradient to zero:
The mean falls out of the first-order condition. It is not a convention — it is the exact minimizer.
Check the second order: the Hessian is , positive definite. This stationary point is a minimum, not a saddle or a maximum.
The interpretation is worth keeping: the mean is the point that minimizes total squared distance to a set of points. That is the entire justification for the update, and it is why the objective is defined with squares in the first place.
Warning: If a cluster receives no points, its mean is undefined. Real implementations handle this by reseeding or dropping the cluster. This is a genuine failure mode, not a footnote.
Knowledge check
Check your understanding
Answer this question before you continue.
Lloyd's Algorithm as Alternating Minimization
Assemble the two derived steps:
- Initialize centroids .
- Assignment: assign each point to its nearest centroid.
- Update: move each centroid to the mean of its assigned points.
- Repeat until assignments stop changing, or centroid movement falls below a tolerance.
This is Lloyd's algorithm. Initialization is a required input, not part of the derivation. The objective says nothing about where to start — which is precisely why the starting point matters so much.
The standard seeding scheme is k-means++: pick the first center at random, then pick each subsequent center with probability weighted toward points far from existing centers. It does not change the objective. It changes which local minimum you tend to land in.
Convergence in practice is checked three ways — assignments unchanged, centroid shift below a threshold, or a maximum iteration cap. All three are stopping heuristics for the same underlying condition.
Cost per iteration is : every point is compared against every centroid. That is why K-means scales, and why and are the levers.
Worked Example: Watch the Objective Fall
Take six points on a line: $1, 2, 3, 10, 11, 12k = 2\mu_1 = 1\mu_2 = 12$.
Step 0 — initial objective. Assign by nearest centroid: to , to .
Step 1 — update. New means: , .
dropped from 10 to 4. The centroids moved to the true centers of their clusters.
Step 2 — assignment. Reassign by nearest centroid. Point 3 is distance 1 from and distance 8 from — stays. Point 10 is distance 8 from and 1 from — stays. No point changes cluster. The algorithm has stopped. Final .
Now rerun with a different initialization: , .
Assign: to , to . Update: , . Same answer. This dataset is too clean to trap the algorithm.
Make it harder. Start with , . Assign: point 1 goes to , points 2 and 3 go to , and points 10, 11, 12 go to as well. Now and .
That is a local minimum the algorithm will settle into — and it is far worse than 4. Same data, same , different answer. The objective tells you which one is worse.
A short scikit-learn check confirms the library minimizes exactly this quantity, exposed as inertia_:
import numpy as np
from sklearn.cluster import KMeans
X = np.array([[1], [2], [3], [10], [11], [12]])
km = KMeans(n_clusters=2, n_init=10, random_state=0).fit(X)
print(km.inertia_) # within-cluster sum of squares
print(km.cluster_centers_) # the means
The derivation is the point. The code is the receipt.
Knowledge check
Check your understanding
Answer this question before you continue.
Why the Guarantee Stops at Local
Here is the convergence argument, stated precisely.
Each step weakly decreases . The objective is bounded below by 0. And there are only finitely many distinct partitions of points into groups. A non-increasing sequence over a finite set must eventually repeat a state. Once a state repeats, the algorithm cycles — but since never increases, the cycle must be a fixed point.
What this proves: the algorithm terminates at a configuration where neither step can improve . That is a local minimum of the alternating-minimization scheme.
What it does not prove: that no other partition achieves a lower . The objective is non-convex, and the descent argument is blind to valleys the algorithm never visited.
The practical consequence is that results depend on initialization. Multiple restarts with different seeds, keeping the lowest final , is the standard mitigation — and it is a mitigation, not a guarantee.
Common mistake: Reading "K-means converged" as "this is the best clustering." Those are different claims. Convergence means this run stopped improving, not this is optimal.
One theoretical footnote worth knowing: Lloyd's algorithm can require super-polynomial time on adversarial inputs, even in two dimensions. Typical inputs behave far better, so treat this as a known worst-case result rather than a practical warning.
What the Derivation Tells You to Do Next
The theory converts into four decision rules.
Always set a random seed and run multiple restarts. Compare final objective values, not just labels.
Scale features before fitting. The objective sums squared distances across dimensions, so an unscaled feature with large variance dominates the cost.
Treat a lower objective as a better fit to the stated cost — nothing more. The objective measures compactness under squared Euclidean distance. It does not measure whether the clusters mean anything.
When the objective plateaus across many values of , that is a signal about the data's geometry, not proof of the right .
The practical companion article runs this in scikit-learn and inspects assignments, centroids, and scaling effects. This article supplied the why; that one supplies the hands-on loop.
Before you move on, re-derive the centroid update from scratch on paper. Set up the sum of squared distances, take the gradient, set it to zero, and watch the mean appear. It takes two minutes, and it permanently converts the mean rule from a memorized fact into something you can reconstruct.
Knowledge check
Final check
Finish the article by checking the ideas you just learned.
References
Build stronger machine learning foundations
Use structured resources to connect theory, scikit-learn workflows, and evaluation practice.


