Prove mathematically why the k-means centroid update is the cluster mean. Then explain what the centroid becomes if you measure distance with L1 instead of squared Euclidean distance.

The Problem: Lloyd’s assignment step has placed five API latencies, 20, 30, 40, 50 and 360 ms, in one cluster, and the update step must now choose the centroid . You must prove which
minimizes squared error, which minimizes absolute error, and whether the whiteboard candidates 40 ms and 100 ms are right.
Answer
With cluster assignments held fixed, the k-means objective splits into one independent problem per cluster: find the point that minimizes the within-cluster sum of squared errors
. Setting the gradient
to zero gives
. The Hessian
is positive definite, so the mean is the unique global minimizer and not just a stationary point. Under L1 distance the cost
separates into one absolute-deviation problem per coordinate, and each is minimized where at least half the values sit on each side, which is the coordinate-wise median. That swap turns k-means into k-medians, trading a closed-form, rotation-invariant update for one that a single outlier cannot drag away.
(1) Fixed Assignments Decouple Clusters: In the full loss each
appears in only one inner sum. The update step therefore solves
separate single-center problems.
(2) First-Order Condition: The gradient vanishes exactly when the residuals sum to zero, which is the defining property of the arithmetic mean.
(3) Convexity Makes It Global: The Hessian is positive definite for any nonempty cluster. The identity
shows every other center pays an exact, quadratic penalty.
(4) L1 Splits By Coordinate: Because , each coordinate is a 1-D problem whose slope equals points on the left minus points on the right. The slope crosses zero at the median.
(5) The Update Must Match The Distance: Lloyd’s monotone decrease holds only because assignment and update minimize the same objective. Assigning by L1 and then updating with the mean can increase the loss.
Mathematical Formulation:
Where:
is the within-cluster SSE of cluster
for center
, where
is member
and
is the cluster size.
is the gradient with respect to the center, using
; setting it to zero forces the residuals to sum to zero.
is the minimizer, the cluster mean.
is the Hessian,
the
identity, and
means positive definite, so
is strictly convex. For an empty cluster (
) the Hessian is zero and the mean is undefined.
is any candidate center, and
is the exact excess cost of choosing it instead of the mean.
is the L1 cost of center
,
indexes coordinates, and
and
are coordinate
of point
and of the center.
is the subdifferential:
when
,
when
, and the interval
at equality.
- The zero condition holds exactly when at least half the points have
and at least half have
. That is the median: unique for odd
, and any value between the two middle order statistics for even
.
Interviewers often follow up by asking for a proof without calculus. Write and expand the square. The cross term sums to
, which is zero because deviations from the mean always sum to zero. What remains is
, the König-Huygens identity (the statistics twin of the parallel-axis theorem). Every center other than the mean pays exactly
extra, so the mean wins with no derivative in sight.
For L1, fix one coordinate and slide right by a small
between two data values. Every point on the left gets
farther away and every point on the right gets
closer, so the cost changes by
. The cost falls while more points lie to the right and rises once more lie to the left, bottoming out where the counts balance. The function is piecewise linear with kinks at the data values, so the optimum sits on a kink where no ordinary derivative exists. That is why the proof needs subgradients, and why implementations compute a median by selection rather than by gradient descent.
Worked Example:
For the cluster 20, 30, 40, 50 and 360 ms, the mean is and the deviations are -80, -70, -60, -50 and 260, so
. At the median 40 the identity predicts
, and the direct sum
agrees. Under L1 the median costs
, while the mean costs
. The slopes of
between data points run -5, -3, -1, +1, +3, +5, so the sign flips exactly at 40. Drop the outlier and the four points 20, 30, 40, 50 have an even count: every
in
costs 40, so the L1 centroid is a whole interval, while the mean is still the single value 35.

Figure 1: The same five latencies under two costs. (a) Squared error is a smooth parabola centered on the mean, 100 ms, and the median pays the exact penalty . (b) Absolute error is piecewise linear with slope equal to points on the left minus points on the right, so it bottoms out at the median, 40 ms, where the slope changes sign.
The choice of distance therefore picks the center, and three nearby algorithms are easy to confuse. Unsquared Euclidean distance gives a fourth center, the geometric median (Weber point), which has no closed form and is usually computed with Weiszfeld iterations. Interviewers probe whether you can keep the coordinate-wise median, the geometric median and the medoid apart. Each one changes what the update step costs and how it reacts to outliers, as the table summarizes.
| Property | k-means | k-medians | k-medoids |
|---|---|---|---|
| Distance | Squared Euclidean | L1 (Manhattan) | Any dissimilarity, even non-metric |
| Optimal center | Arithmetic mean | Coordinate-wise median | Member with the least total dissimilarity |
| Update cost per cluster | O(nd), closed form | O(nd) with quickselect, O(nd log n) with sorting | O(n^2 d) for a naive search over members |
| Uniqueness | Always unique for a nonempty cluster | Unique for odd n, an interval per coordinate for even n | Ties between members are possible |
| Outlier robustness | Breakdown point 0: one point can move it arbitrarily far | Breakdown point 50% per coordinate | Robust, and the center is always a real member |
| Rotation invariance | Yes | No, the result depends on the coordinate axes | Inherits the invariance of the chosen dissimilarity |
| Center is a data point | Generally no | Generally no: (0,0), (1,5), (5,1) give (1,1) | Always yes |
Leave a Reply