k-Means Clustering

kk-Means is an unsupervised partition-based clustering algorithm that divides a dataset of nn observations into kk distinct, non-overlapping clusters where each observation belongs to the cluster with the nearest mean (centroid).

Objective Function

The objective is to minimize the Within-Cluster Sum of Squares (WCSS), also known as Inertia:

J=∑j=1k∑i∈Cj∥xi−μj∥2J = \sum_{j=1}^{k} \sum_{i \in C_j} \| x_i - \mu_j \|^2

Where:

  • kk is the number of clusters.
  • CjC_j is the set of points belonging to cluster jj.
  • μj\mu_j is the centroid (mean vector) of cluster jj.
  • ∥xi−μj∥2\| x_i - \mu_j \|^2 is the squared Euclidean distance between point xix_i and centroid μj\mu_j.

Algorithm Steps (Lloyd's Algorithm)

  1. Initialization: Select kk initial centroids μ1,μ2,…,μk\mu_1, \mu_2, \dots, \mu_k.
  2. Assignment Step: Assign each data point xix_i to the closest centroid: c(i)=arg⁡min⁡j∥xi−μj∥2c^{(i)} = \arg\min_j \| x_i - \mu_j \|^2
  3. Update Step: Recompute the centroid of each cluster as the mean of all points assigned to it: μj=1∣Cj∣∑i∈Cjxi\mu_j = \frac{1}{|C_j|} \sum_{i \in C_j} x_i
  4. Convergence Check: Repeat steps 2 and 3 until centroids stabilize (movement <ϵ< \epsilon) or maximum iterations are reached.

Initialization Strategies

  • Random Initialization: Randomly chooses kk data points as initial centroids. Prone to converging to poor local optima.
  • kk-Means++: Chooses the first centroid uniformly at random, then chooses subsequent centroids with probability proportional to the squared distance to the nearest existing centroid (D(x)2D(x)^2). This ensures well-spaced initial centroids and substantially faster convergence.

Determining Optimal kk

  1. Elbow Method: Plot Inertia (WCSS) against different values of kk. Look for the "elbow point" where the rate of decrease sharpens to a plateau.
  2. Silhouette Score: Measures how similar a point is to its own cluster compared to other clusters (values range from −1-1 to +1+1, where higher indicates well-separated clusters): s(i)=b(i)−a(i)max⁡(a(i),b(i))s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))} where a(i)a(i) is mean intra-cluster distance and b(i)b(i) is mean nearest-cluster distance.

Python Example (scikit-learn)

from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# Standardize features (crucial for distance-based algorithms)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Fit KMeans with k-means++ initialization
kmeans = KMeans(n_clusters=3, init='k-means++', random_state=42, n_init=10)
labels = kmeans.fit_predict(X_scaled)
centroids = kmeans.cluster_centers_

Strengths & Limitations

  • Strengths:
    • Simple, scalable (O(n⋅k⋅d⋅i)O(n \cdot k \cdot d \cdot i) time complexity), and easy to interpret.
    • Guaranteed to converge to a local optimum.
  • Weaknesses:
    • Assumes spherical, equally sized clusters with isotropic variance.
    • Vulnerable to outliers (which distort the cluster means).
    • Struggles with complex, non-convex cluster geometries (better handled by DBSCAN or Spectral Clustering).
    • Requires specifying kk in advance.
Loading...