Machine Learning & AI
Intermediate

K-Means Objective (WCSS)

Clustering minimizes the total squared distance of points to their cluster centre.

Formula

J=∑k=1K∑x∈Ck∥x−μk∥2J = \sum_{k=1}^{K}\sum_{\mathbf{x}\in C_k}\lVert \mathbf{x}-\mu_k\rVert^2

Variables

C_kCluster k
\mu_kCentroid
KNumber of clusters

Example

Lower WCSS means tighter clusters

How it works

This formula measures how compact a K-means clustering is by adding up the squared distances from every point to the centroid of the cluster it belongs to. Because the distances are squared, points that are far from their centroid are penalized more heavily, so the algorithm is pushed toward groups with small spread rather than just a few close points and some distant outliers. K-means works by repeatedly assigning points to the nearest centroid and then updating each centroid to the mean of its assigned points, which reduces this objective until it reaches a stable minimum.

When to use it

Use this when you want to evaluate or fit a K-means clustering model and compare different values of K. It is common in customer segmentation, image compression, document grouping, and any task where you want clusters that are internally tight.

Worked example, step by step

Suppose K = 2 with clusters C1 = {(1, 2), (2, 1)} and C2 = {(6, 5), (7, 4)}. First find the centroid of C1: μ1 = ((1 + 2)/2, (2 + 1)/2) = (1.5, 1.5). Now compute squared distances in C1: (1,2) to (1.5,1.5): (1−1.5)^2 + (2−1.5)^2 = 0.25 + 0.25 = 0.5 (2,1) to (1.5,1.5): (2−1.5)^2 + (1−1.5)^2 = 0.25 + 0.25 = 0.5 So WCSS for C1 = 0.5 + 0.5 = 1.0. Find the centroid of C2: μ2 = ((6 + 7)/2, (5 + 4)/2) = (6.5, 4.5). Squared distances in C2: (6,5) to (6.5,4.5): (6−6.5)^2 + (5−4.5)^2 = 0.25 + 0.25 = 0.5 (7,4) to (6.5,4.5): (7−6.5)^2 + (4−4.5)^2 = 0.25 + 0.25 = 0.5 So WCSS for C2 = 0.5 + 0.5 = 1.0. Total J = 1.0 + 1.0 = 2.0.

Common mistakes to avoid

  • Using the distance to the wrong centroid instead of the centroid for each point's assigned cluster
  • Forgetting that the Euclidean distance is squared, not just the raw distance
  • Mixing up the centroid with a data point rather than calculating it as the mean of the cluster

Did You Know?

K-means alternates assigning points and moving centroids — a special case of expectation-maximization.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026