Load packages library(tidyverse) The K-Means optimization reduces the variance in each iteration. To illuminate on that Witten et al. in An Introduction to Statistical Learning (2013) present the following entity (p. 388, chap. 10): [\frac{1}{|Ck|} \sum\limits{i,i^{\prime} \in Ck} \sum\limits{j=1}^p (x{ij} - x{i^\prime j})^2 = 2 \sum\limits_{i \in Ck} \sum\limits{j=1}^{p} (x{ij} - \bar{x}{kj})^2] A proof can be found here; I’ll add some explanations. Note 1. Note that (\sum\limits_{i,i^{\prime} \in Ck}(\dots)) essentially amounts to (\sum\limits{i \in Ck}\sum\limits{i^{\prim …