Mean Shift Clustering
The problem of picking k
Every clustering algorithm covered so far — k-means, spectral clustering — requires you to decide the number of clusters k before running the algorithm. Sometimes that's a reasonable ask. Often it isn't: you genuinely don't know how many natural groups exist in a dataset, and guessing wrong biases the result before you've even started.
Mean Shift clustering doesn't ask for k at all. Instead, it treats the dataset as samples drawn from some underlying probability density, and it finds clusters by locating the modes — the local peaks — of that density. The number of modes found is the number of clusters, discovered automatically from the data's actual shape.
The algorithm: climbing the density hill
Mean Shift works by placing a window (defined by a bandwidth parameter) around every data point and iteratively sliding that window toward the region of highest density nearby — literally walking uphill on the estimated density surface.
1. Start a window centered at each data point (or a representative subsample).
2. Compute the mean of all points falling within the window (typically weighted by a kernel, most commonly Gaussian, so nearer points count more).
3. Shift the window's center to that mean.
4. Repeat steps 2-3 until the window stops moving — it has reached a local density peak (a mode).
5. Group points whose windows converged to the same (or a very close) mode into one cluster.
Every point effectively 'rolls uphill' through the density landscape until it settles at a peak, and points that settle at the same peak belong to the same cluster.
Note
Contrast with the k-means visualization
In the K-Means visualization, you choose k, then watch centroids move toward the mean of their *currently assigned* points — the number of clusters never changes during the run. Mean shift inverts this: there's no fixed set of centroids to begin with, no pre-assigned k, and no discrete assignment step at all. Every point independently climbs its local density gradient, and the final cluster count simply falls out of however many distinct peaks the data happens to have. Picture the same blob dataset from the k-means demo — mean shift would arrive at the same clusters without ever being told there were three of them.
📊 K-Means Clustering Simulator
InteractiveK-means vs. mean shift
| Property | K-Means | Mean Shift |
|---|---|---|
| Number of clusters | Must be specified as k | Discovered automatically from density peaks |
| Key hyperparameter | k | Bandwidth (window radius) |
| Cluster shape assumption | Convex, spherical | Arbitrary shape, follows density contours |
| Sensitivity to outliers | High — pulls centroids off-center | Lower — low-density regions form no peak |
| Computational cost | Fast, O(n · k · iterations) | Slower, each point iterates its own hill-climb |
Note
What's next
Mean shift finds clusters by density, which naturally leaves sparse, low-density points unassigned to any strong peak — a useful signal in its own right. Local Outlier Factor builds directly on the idea of local density to detect anomalies: points sitting in a region far sparser than their neighbors.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.