k-Means Clustering

Grouping unlabelled data into k clusters by moving centres back and forth.

🟢 beginner5 min readunsupervised
K-means groups data into k clusters without any labels. It places k centre points, assigns every point to its nearest centre, then moves each centre to the middle of the points assigned to it, repeating until nothing changes. It is fast and simple, but you must choose k yourself, a bad starting position can give a poor result, which the k-means++ starting method largely fixes, and it assumes clusters are roughly round and similar in size.

What is k-Means Clustering?

k-Means is the most popular unsupervised clustering algorithm.

Its goal is to group unlabelled data points into kk distinct clusters where points in the same cluster are as close to each other as possible, while points in different clusters are far apart.

  UNLABELLED DATA ──► [ K-MEANS CLUSTERING (k=3) ] ──► DISCOVERED CLUSTERS:
                                                        - Cluster 1 (Red Centroid)
                                                        - Cluster 2 (Blue Centroid)
                                                        - Cluster 3 (Green Centroid)

Lloyd's Algorithm (Step-by-Step)

To find kk clusters, Lloyd's algorithm executes four steps:

  1. INITIALIZATION: Pick k starting centroid points in feature space.
        │
        ▼
  2. ASSIGNMENT STEP: Assign each data point to its CLOSEST centroid (Euclidean distance).
        │
        ▼
  3. UPDATE STEP: Recalculate each centroid position as the MEAN of all assigned points.
        │
        ▼
  4. REPEAT Assignment + Update steps until centroids STOP MOVING (Convergence!).

Objective Function: Inertia (WCSS)

k-Means minimizes Within-Cluster Sum of Squares (Inertia):

J=∑j=1k∑x∈Sj∥x−μj∥2J = \sum_{j=1}^k \sum_{x \in S_j} \| x - \mu_j \|^2

Inertia measures how tightly packed data points are around their assigned cluster centroids.

The k-Means++ Initialization Smart Trick

Standard random centroid initialization can place two starting centroids right next to each other, trapping the algorithm in bad local minima.

k-Means++ (Arthur & Vassilvitskii, 2007) fixes this:

  1. Pick the first centroid μ1\mu_1 randomly from the data points.
  2. For each remaining data point xx, calculate distance D(x)D(x) to the nearest already chosen centroid.
  3. Choose the next centroid with probability proportional to D(x)2D(x)^2.

This ensures initial centroids start spread far apart across data space, speeding up convergence and guaranteeing O(log⁡k)O(\log k) optimal cost bounds.

Limitations of k-Means

  1. Assumes Spherical Clusters: Fails completely on non spherical shapes (e.g. concentric circles or elongated ribbons). Use DBSCAN or Spectral Clustering instead.
  2. Requires Specifying k Upfront: You must manually choose cluster count kk (use the Elbow Method or Silhouette Analysis).
  3. Sensitive to Outliers: A single extreme outlier drags the centroid mean far off center.

Say this out loud

k-Means partitions unlabelled data into k clusters by alternating between assigning points to the nearest centroid and updating centroids to be the mean of assigned points. It minimizes Within Cluster Sum of Squares. k-means++ smart initialization spreads starting centroids far apart to avoid bad local minima. k-Means assumes spherical clusters and requires feature scaling.

Followups to expect

  1. What is Mini Batch k-Means? An optimized variant that updates centroids using small random mini batches of data points instead of the full dataset, running 10x faster on massive datasets.
  2. Why is Feature Scaling mandatory before k-Means? Because k-Means measures distance using Euclidean distance ∥x−μ∥2\|x - \mu\|^2. Features with large scales dominate distance calculations, distorting cluster centroids.

Check yourself

Question 1 of 3

What two alternating steps comprise Lloyd's Algorithm inside k-Means Clustering?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min