What objective function does k-means optimize?
Last updated
K-means minimizes the within-cluster sum of squares (WCSS, also called inertia): the total squared Euclidean distance from each point to the centroid of its assigned cluster.
[lang:text]
J = Σ_{k=1..K} Σ_{x_i ∈ C_k} || x_i − μ_k ||², μ_k = mean of points in C_k
What it implies:
For fixed assignments, the mean is the point minimizing squared distance, which is why centroids are means.
Equivalent to minimizing within-cluster variance, and (since total variance is fixed) maximizing between-cluster variance.
Favors compact, roughly spherical, similar-sized clusters.
Optimization properties:
Finding the global minimum is NP-hard; Lloyd's algorithm only reaches a local minimum.
Each assignment and update step never increases J, guaranteeing convergence.
In scikit-learn the final value is
KMeans.inertia_; it always decreases with more clusters, so it can't choose k alone (hence elbow/silhouette).