K-Means Clustering

K-Means is a clustering algorithm that finds cluster centers and assigns each data point to one cluster , such that the total within-cluster squared error is given by the objective function

The optimal clusters are found programmatically using Lloyd’s Algorithm. Start by randomly choosing data points as initial cluster centers. Then, repeat until until cluster assignments stop changing, such that :

  1. Assign each data point to its nearest cluster center , such that
  2. Recompute each cluster center as the mean of its assigned points:

K-Means is guaranteed to converge, because alternating minimization of assignments () and centers () never increases the objective. Most often, the hyperparameter is chosen using the elbow method by plotting the K-Means objective vs. and choosing the point where improvement sharply decreases (i.e., “the elbow”).

Proof

Mathematical Derivation (Optional Detail)

To minimize

\sum_{n=1}^N |x_n - \mu_{z_n}|2

we rearrange by cluster:

\sum{k=1}^{K}\sum_{i=1}^{N_k}\sum_{d=1}^{D}(x_{k_i,d}-\mu_{k,d})

Taking the derivative w.r.t. \mu_{k,d} and setting to zero:

\mu_{k,d} = \frac{1}{N_k}\sum_{i=1}

→ Each cluster center is the mean of its assigned points.

Prove Lloyd correctness and convergence guarantee