Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

K-Means Clustering

Objective

K-means partitions nn data points into KK clusters by minimizing the total within-cluster squared distance:

min⁡{ck},rJ(c,r)=∑i=1n∑k=1Krik∥xi−ck∥2\min_{\{c_k\}, r} J(c, r) = \sum_{i=1}^n \sum_{k=1}^K r_{ik} \|x_i - c_k\|^2

subject to:

rik∈{0,1},∑k=1Krik=1r_{ik} \in \{0, 1\}, \quad \sum_{k=1}^K r_{ik} = 1

where rik=1r_{ik} = 1 if data point ii is assigned to cluster kk.

Lloyd’s Algorithm

  1. Initialize: choose KK centroids randomly

  2. Assignment step: assign each point to the nearest centroid

    k∗=arg⁡min⁡k∥xi−ck∥2,rik∗=1k^* = \arg\min_{k} \|x_i - c_k\|^2, \quad r_{ik^*} = 1
  3. Update step: recompute each centroid as the mean of its assigned points

    ck=∑i=1nrikxi∑i=1nrikc_k = \frac{\sum_{i=1}^n r_{ik} x_i}{\sum_{i=1}^n r_{ik}}
  4. Repeat until assignments no longer change

Optimization Perspective: Block Coordinate Descent

The K-means algorithm is block coordinate descent on the objective J(c,r)J(c, r):

The refitting step has a closed-form solution by setting the derivative to zero:

∂J∂ck=−2∑i=1nrik(xi−ck)=0  ⟹  ck=∑irikxi∑irik\frac{\partial J}{\partial c_k} = -2\sum_{i=1}^n r_{ik}(x_i - c_k) = 0 \implies c_k = \frac{\sum_{i} r_{ik} x_i}{\sum_{i} r_{ik}}

Convergence

K-means converges in finitely many steps because:

However, the objective is non-convex, so the algorithm may converge to a local minimum. Finding the global optimum is NP-hard in general.

Initialization and Variants

VariantDescription
Multiple restartsRun K-means multiple times with random initializations; pick the solution with smallest JJ
K-means++Smart initialization: choose first centroid randomly, then choose subsequent centroids with probability proportional to squared distance from nearest existing centroid
Mini-batch K-meansUpdate centroids using only a small random subset at each iteration (faster for large datasets)
K-mediansUse ℓ1\ell_1 distance and median instead of mean (more robust to outliers)
K-medoidsRepresent each cluster by an actual data point (more robust)
Fuzzy C-meansAllow soft assignments with membership weights

Empty clusters: If no points are assigned to a cluster, reinitialize the centroid to a random data point or the point farthest from its current centroid.

Performance Evaluation

Internal metric — Silhouette Coefficient:

For a given data point, define:

s=b−amax⁡(a,b)={1−ab,a<b0,a=bba−1,a>bs = \frac{b - a}{\max(a, b)} = \begin{cases} 1 - \frac{a}{b}, & a < b \\ 0, & a = b \\ \frac{b}{a} - 1, & a > b \end{cases}

s∈[−1,1]s \in [-1, 1]: large positive means well-clustered; near 0 means near boundary; negative means possibly wrong cluster.

External metric — Rand Index (RI):

Compare two partitions XX and YY of nn samples. Count over all unordered pairs:

RI=a+ba+b+c+d=a+b(n2)\text{RI} = \frac{a + b}{a + b + c + d} = \frac{a + b}{\binom{n}{2}}

Adjusted Rand Index (ARI): corrects RI for chance agreement.

ARI=∑i,j(nij2)−[∑i(ai2)][∑j(bj2)]/(n2)12[∑i(ai2)+∑j(bj2)]−[∑i(ai2)][∑j(bj2)]/(n2)\text{ARI} = \frac{\sum_{i,j} \binom{n_{ij}}{2} - \left[\sum_i \binom{a_i}{2}\right]\left[\sum_j \binom{b_j}{2}\right]/\binom{n}{2}}{\frac{1}{2}\left[\sum_i \binom{a_i}{2} + \sum_j \binom{b_j}{2}\right] - \left[\sum_i \binom{a_i}{2}\right]\left[\sum_j \binom{b_j}{2}\right]/\binom{n}{2}}

ARI = 1 means perfect agreement; near 0 means random agreement.

Choosing K

We cannot simply minimize JJ since it always decreases as KK increases. Common approaches: