Skip to content
AI360Xpert
Core ML
Visual explainer

K-Means Clustering

How Lloyd's algorithm discovers groups in unlabelled data through a two-step dance of assigning points and moving centroids.

Unlabelled data forms natural groups based on proximity.
Unlabelled data forms natural groups based on proximity.

In unsupervised learning, data arrives without labels. We only have coordinates. The goal is to discover the natural groupings hidden in the geometry of the data.

Step 1: Assign

Step 1: Place random centroids and assign every point to its closest centroid.
Step 1: Place random centroids and assign every point to its closest centroid.

We begin by guessing where the centres might be, dropping random centroids into the space. Every point is then coloured according to whichever centroid is closest to it.

Step 2: Move

Step 2: Move each centroid to the centre of its newly assigned points.
Step 2: Move each centroid to the centre of its newly assigned points.

The initial guess was wrong. To improve it, we calculate the exact geometric centre (the mean) of each newly formed cluster. The centroids are moved to these new coordinates.

Convergence

Convergence: The centroids stop moving when no point changes its assignment.
Convergence: The centroids stop moving when no point changes its assignment.

We repeat the two steps: assign points to the nearest centroid, then move the centroid to the mean. The dance stops when moving the centroids no longer causes any point to change its assignment.

Where It Breaks

K-means assumes spherical clusters. It slices complex shapes in half.
K-means assumes spherical clusters. It slices complex shapes in half.

Because K-means measures distance straight from a centre point, it inherently assumes clusters are spherical. If the true groups form rings, moons, or irregular densities, K-means will draw a straight line right through them.

The Quick Version

  • No labels: Discovering structure using only position.
  • Assign: Points belong to their closest centroid.
  • Move: Centroids shift to the mathematical average.
  • Converge: The loop ends when assignments stabilise.
  • Failure: Breaks entirely on non-spherical shapes.

What to Read Next