Mini-Batch K-Means

medium
Unit 9 · Clustering & Classification0/2 to milestone

Full k-means touches every point each iteration. Mini-batch k-means updates centroids from small random batches with a per-centroid learning rate.

Implement minibatch_kmeans(X, init_centroids, batch=32, iters=100, seed=0):

  • keep a count n_c of points ever assigned to each centroid (start at 0)
  • each iteration: sample batch row indices with rng.integers(0, n, batch) (one np.random.default_rng(seed)), assign each sampled point to its nearest centroid, then for each sampled point in order: n_c += 1, c += (x - c) / n_c

Return the final centroids (copy the initial ones, don't modify the input).

#clustering#k-means#scalability2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Mini-Batch K-Means · TechGradify