Mean-Shift Clustering

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 9. Everything before it counts as cleared.

Mean shift finds clusters without being told how many there are: every point climbs uphill on the estimated density until it reaches a peak (a mode), and points that reach the same mode form a cluster.

Implement mean_shift(X, bandwidth, tol=1e-5, max_iter=300):

  1. Start a copy of every point. Repeatedly move each copy to the Gaussian-weighted mean of the original points, with weights exp⁡(−∥x−c∥2/(2h2))\exp(-\lVert x - c\rVert^2 / (2h^2)), until every copy moves less than tol (or max_iter is reached).
  2. Merge modes: scan the converged points in order. A point joins the first existing centre within bandwidth / 2; otherwise it becomes a new centre.
  3. Return (centres, labels), with centres as an array in order of creation.
#clustering#density#unsupervised2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Mean-Shift Clustering · TechGradify