Solution (source code)

= Solution

Write $n_k=|C_k|$ and suppose the cluster is nonempty. Expanding the <Euclidean norm> through its <inner product> gives
$$
\|x-z\|^2=\|(x-\bar x_k)-(z-\bar x_k)\|^2
=\|x-\bar x_k\|^2-2(x-\bar x_k)^{\mathsf T}(z-\bar x_k)
+\|z-\bar x_k\|^2.
$$
Sum over $x\in C_k$. The final term does not depend on $x$, so its sum is $n_k\|z-\bar x_k\|^2$, which proves the requested identity. This expansion is the starting point for the <centroid sum-of-squares identity>.