VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 15: The ML coding round

Lesson 15.4 of 12 in this module Unrolled Pro

k-means from scratch

Cluster n points into k groups by alternating two steps: assign each point to its nearest centroid, then move each centroid to the mean of its points. From Python loops over points and centroids (O(n · k · d) interpreted work per iteration), to a vectorised assignment with the distance-matrix expansion and argmin, a centroid update with one scatter-add, a stopping rule, empty-cluster handling, and k-means++ initialisation. Then mini-batch k-means for large n, choosing k, cosine k-means for embeddings, and k-means as the coarse quantiser of a vector index.

about 10 min 3 Python examples 2 quick checks

Problem. Given points X (n × d) and k, find k centroids that minimise the inertia: the sum of squared distances from each point to its nearest centroid. Use Lloyd's algorithm:

  1. Assign: each point goes to its nearest centroid.
  2. Update: each centroid moves to the mean of its assigned points.
  3. Repeat until the assignments stop changing (or a maximum number of iterations).

Both steps can only lower (or keep) the inertia: assigning to the nearest centroid can't increase any point's distance, and the mean is the point that minimises the sum of squared distances to a set. So the loop always converges, though possibly to a local minimum that depends on the starting centroids.

ML context. k-means is the default clustering baseline, and much more: it builds the coarse quantiser of IVF vector indexes (lesson 15.2's Change C), the codebooks of product quantisation and vector-quantised models, colour palettes, and data-deduplication clusters. In an ML round you're expected to vectorise both steps, handle empty clusters, and know why initialisation matters.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "k-means from scratch"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee