DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

K-Means From Scratch: Finding Groups With No Labels

Most ML you meet is supervised — it learns from labeled examples. K-Means is the opposite: hand it unlabeled points and it finds the groups on its own. And the algorithm is almost shockingly simple. Here it is, running live on a 2D canvas.

🎯 Watch it cluster (step or run to convergence): https://dev48v.infy.uk/ml/day13-kmeans.html

Two steps, repeated

  1. Assign: colour each point by its nearest centre.
  2. Update: move each centre to the average of the points assigned to it.

Repeat. That's it. The centres drift into the middle of natural clusters and stop moving — that's convergence.

What it's minimizing

Each iteration lowers the total within-cluster distance (inertia). It's guaranteed to stop, though it can land in a local minimum — which is why initialization (k-means++) and running a few times matter.

The catches

You pick K up front (the elbow method helps). It assumes roundish, similar-size clusters, and you should scale your features first. Used everywhere: customer segments, image color compression, grouping anomalies.

🔨 Built from scratch (init → assign → update → loop → inertia) on the page: https://dev48v.infy.uk/ml/day13-kmeans.html

Part of MachineLearningFromZero. 🌐 https://dev48v.infy.uk

Top comments (0)