ML0188 Why K-Means Uses the Mean

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.

Three-panel scenario card. Panel 1 shows one k-means cluster after the assignment step: five API latency readings on a number line from 0 to 380 ms, four bunched at 20, 30, 40 and 50 ms and one far away at 360 ms, with a note that only the centroid c is unknown. Panel 2 shows two ways to score a centroid: squared error J2 of c equal to the sum of squared differences, and absolute L1 error J1 of c equal to the sum of absolute differences. Panel 3 asks which c minimizes each cost and why in any dimension, with candidates c equal to 40 ms, c equal to 100 ms, or neither.

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 c. You must prove which c 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 \mu that minimizes the within-cluster sum of squared errors \sum_i \|x_i - \mu\|^2. Setting the gradient -2\sum_i (x_i - \mu) to zero gives \mu = \frac{1}{n}\sum_i x_i. The Hessian 2nI is positive definite, so the mean is the unique global minimizer and not just a stationary point. Under L1 distance the cost \sum_i \|x_i - m\|_1 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 \sum_k \sum_{i \in C_k} \|x_i - \mu_k\|^2 each \mu_k appears in only one inner sum. The update step therefore solves k separate single-center problems.
(2) First-Order Condition: The gradient -2\sum_i (x_i - \mu) vanishes exactly when the residuals sum to zero, which is the defining property of the arithmetic mean.
(3) Convexity Makes It Global: The Hessian 2nI is positive definite for any nonempty cluster. The identity J(c) = J(\bar x) + n\|c - \bar x\|^2 shows every other center pays an exact, quadratic penalty.
(4) L1 Splits By Coordinate: Because \|x - m\|_1 = \sum_j |x_j - m_j|, 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:
J(\mu) = \sum_{i \in C} \|x_i - \mu\|_2^2
\nabla_\mu J = -2 \sum_{i \in C} (x_i - \mu) = 0
\mu^* = \bar x = \frac{1}{n} \sum_{i \in C} x_i
\nabla_\mu^2 J = 2n I \succ 0
J(c) = J(\bar x) + n \|c - \bar x\|_2^2
J_1(m) = \sum_{j=1}^{d} \sum_{i \in C} |x_{ij} - m_j|
0 \in \sum_{i \in C} \partial_{m_j} |x_{ij} - m_j|
m_j^* = \mathrm{median}\{x_{ij} : i \in C\}

Where:

  • J(\mu) is the within-cluster SSE of cluster C for center \mu, where x_i \in \mathbb{R}^d is member i and n = |C| \geq 1 is the cluster size.
  • \nabla_\mu J is the gradient with respect to the center, using \nabla_\mu \|x - \mu\|^2 = -2(x - \mu); setting it to zero forces the residuals to sum to zero.
  • \mu^* = \bar x is the minimizer, the cluster mean. \nabla_\mu^2 J is the Hessian, I the d \times d identity, and \succ 0 means positive definite, so J is strictly convex. For an empty cluster (n = 0) the Hessian is zero and the mean is undefined.
  • c is any candidate center, and n\|c - \bar x\|_2^2 is the exact excess cost of choosing it instead of the mean.
  • J_1(m) is the L1 cost of center m, j \in \{1, \ldots, d\} indexes coordinates, and x_{ij} and m_j are coordinate j of point i and of the center.
  • \partial_{m_j}|x_{ij} - m_j| is the subdifferential: -1 when x_{ij} > m_j, +1 when m_j > x_{ij}, and the interval [-1, 1] at equality.
  • The zero condition holds exactly when at least half the points have x_{ij} \leq m_j and at least half have x_{ij} \geq m_j. That is the median: unique for odd n, and any value between the two middle order statistics for even n.

Interviewers often follow up by asking for a proof without calculus. Write x_i - c = (x_i - \bar x) + (\bar x - c) and expand the square. The cross term sums to 2(\bar x - c)^\top \sum_i (x_i - \bar x), which is zero because deviations from the mean always sum to zero. What remains is J(c) = J(\bar x) + n\|c - \bar x\|^2, the König-Huygens identity (the statistics twin of the parallel-axis theorem). Every center other than the mean pays exactly n\|c - \bar x\|^2 extra, so the mean wins with no derivative in sight.

For L1, fix one coordinate and slide m_j right by a small \epsilon between two data values. Every point on the left gets \epsilon farther away and every point on the right gets \epsilon closer, so the cost changes by \epsilon(n_{\mathrm{left}} - n_{\mathrm{right}}). 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 500/5 = 100 and the deviations are -80, -70, -60, -50 and 260, so J(100) = 6400 + 4900 + 3600 + 2500 + 67600 = 85000. At the median 40 the identity predicts 85000 + 5 \times 60^2 = 103000, and the direct sum 400 + 100 + 0 + 100 + 102400 agrees. Under L1 the median costs 20 + 10 + 0 + 10 + 320 = 360, while the mean costs 80 + 70 + 60 + 50 + 260 = 520. The slopes of J_1 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 m in [30, 40] costs 40, so the L1 centroid is a whole interval, while the mean is still the single value 35.

Two-panel chart for the cluster 20, 30, 40, 50 and 360 ms over candidate centroids from 0 to 200 ms. Left: the squared-error cost is a smooth parabola equal to 85,000 plus 5 times c minus 100 squared, with its minimum of 85,000 at the mean of 100 ms and a value of 103,000 at the median of 40 ms. Right: the absolute-error cost is piecewise linear with slopes -5, -3, -1, +1 and +3 between data points, reaching its minimum of 360 at the median of 40 ms and 520 at the mean of 100 ms.

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 5 \times 60^2 = 18000. (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.

Propertyk-meansk-mediansk-medoids
DistanceSquared EuclideanL1 (Manhattan)Any dissimilarity, even non-metric
Optimal centerArithmetic meanCoordinate-wise medianMember with the least total dissimilarity
Update cost per clusterO(nd), closed formO(nd) with quickselect, O(nd log n) with sortingO(n^2 d) for a naive search over members
UniquenessAlways unique for a nonempty clusterUnique for odd n, an interval per coordinate for even nTies between members are possible
Outlier robustnessBreakdown point 0: one point can move it arbitrarily farBreakdown point 50% per coordinateRobust, and the center is always a real member
Rotation invarianceYesNo, the result depends on the coordinate axesInherits the invariance of the chosen dissimilarity
Center is a data pointGenerally noGenerally no: (0,0), (1,5), (5,1) give (1,1)Always yes

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *