Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Walk uphill to the nearest crowd
Imagine the data as a landscape whose height is how crowded each spot is. Mean shift puts a walker on every data point. Each walker looks around, within a radius set by the bandwidth, and steps to the average position of the points it sees. Since more points lie uphill, that step goes uphill. Walkers end on hilltops, and everyone who ends on the same hilltop is one cluster. You never choose k; the landscape decides.
Mean shift is a workhorse of computer vision. It powers image segmentation and the classic mean-shift and CAMShift object trackers used on robots and drones. It also finds the modes of a distribution without assuming how many there are, for example the dominant operating points of an engine.
- 1. Each mean-shift step moves a point to…
- 2. Clusters correspond to…
- 3. The key parameter is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.One step by hand
Gaussian weights w(xᵢ) = exp(−‖x − xᵢ‖²/(2h²)). For x = 0, data 0, 1, 2 and h = 1, the weights are 1, e^(−0.5) = 0.607 and e^(−2) = 0.135.
A Gaussian step. From x = 0 with data 0 and 1 (h = 1), the new position is (1·0 + 0.607·1)/(1 + 0.607) = 0.378. The point moves toward the other data point.
A flat-kernel step. Take the plain mean of the points within h. From x = 0 with data 0, 1, 2, 9 and h = 2, the mean of 0, 1, 2 is 1.
A fixed point. From x = 0 with data −1 and 1, the step stays at 0: symmetric pulls cancel. That is a mode (or a saddle) of the density.
The steps in plain English:
- Choose a kernel (Gaussian or flat) and bandwidth h.
- Start a copy yᵢ at each data point.
- Repeatedly replace yᵢ by the kernel-weighted mean of all data around it until it stops moving.
- Merge converged yᵢ that lie within about h/2 of each other into one mode. Points sharing a mode form a cluster.
- 1. Gaussian weight at distance 1 with h = 1?
- 2. Flat-kernel step from 0 with data 0, 2, 3, 8 and h = 3?
- 3. A point midway between two equal data points…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Why it climbs, and the bandwidth
Gradient ascent with an adaptive step. Let the kernel density estimate be f̂(x) = (1/n)Σ K(‖x − xᵢ‖²/h²). Its gradient is proportional to (Σ wᵢ)(m(x) − x), where m(x) is the weighted mean. So the mean-shift vector m(x) − x points along ∇f̂, and the step equals ∇f̂ divided by a local normaliser. Steps are big where the density is low and small near a peak, with no learning rate to tune.
For kernels with convex, decreasing profiles (Gaussian included), the density increases at every step (Comaniciu and Meer, 2002). Exercise 1 confirmed it for every trajectory: the mean density along the paths rose from 0.135 to 0.190 in 29 iterations, ending at 3 modes within 0.14 of the true centres.
The bandwidth is everything.
- Small h gives a bumpy f̂ with dozens of spurious modes.
- Large h merges real clusters, and in the end everything becomes one peak.
Exercise 2 had four blobs, two of them close together. The best h reached ARI 0.96, while
- scikit-learn’s estimate_bandwidth (average distance to the 30%-quantile neighbour) chose h = 3.4: one mode, ARI 0;
- Silverman’s rule chose h = 1.06: 3 modes, ARI 0.70.
The “stable plateau” heuristic picks the h range where the number of modes stays constant longest. It picked k = 3 here, merging the close pair. The most stable scale is not always the right one.
Cost. Each iteration is O(n²) for all points. Speed-ups: run from a grid or a sample of seeds (bin seeding), use a flat kernel with a k-d tree, or use blurring mean shift.
from sklearn.cluster import MeanShift, estimate_bandwidth
h = estimate_bandwidth(X, quantile=0.3) # a starting point only; check it
ms = MeanShift(bandwidth=h, bin_seeding=True).fit(X)
ms.cluster_centers_, ms.labels_- 1. The mean-shift vector points along…
- 2. Mean-shift steps are large where…
- 3. In exercise 2, scikit-learn's estimate_bandwidth gave…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Convergence. For a Gaussian kernel the sequence of positions converges, and the density increases monotonically; the limit is a stationary point of f̂ (Carreira-Perpiñán, 2007). Stationary points can in principle be saddles, but these are unstable and rarely reached. In practice the limits are modes.
Mean shift as EM. For a Gaussian kernel, mean shift is an EM algorithm. The E-step computes responsibilities of the data points for the current position, and the M-step takes their weighted mean. That explains the monotone increase and the linear convergence rate.
Mode-seeking versus partitioning. k-Means fixes k and finds centroids. Mean shift fixes a scale and finds modes. The number of clusters equals the number of modes of f̂ at bandwidth h, which falls (not always monotonically) as h grows. In 1-D with a Gaussian kernel it is non-increasing (Silverman’s mode test uses this). In higher dimensions new modes can appear as h increases.
Interview questions.
- What do you tune in mean shift? Only the bandwidth (and the kernel). Choose it by domain scale, cross-validated density estimation, or inspecting cluster counts over a range, not blindly by a rule.
- Why is mean shift popular for tracking? Tracking needs the mode of a colour-histogram similarity surface near the previous position, and mean shift finds it in a few cheap iterations without a global search.
- 1. With a Gaussian kernel, mean shift is equivalent to…
- 2. In 1-D with a Gaussian kernel, the number of modes as h grows…
- 3. Cost of one full mean-shift iteration on n points?
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Can the bandwidth be chosen automatically?
History.
- K. Fukunaga and L. Hostetler introduced mean shift (1975).
- Y. Cheng (1995) generalised it and showed its link to clustering and optimisation.
- D. Comaniciu and P. Meer (2002) made it a standard tool for image segmentation and tracking.
- M. Carreira-Perpiñán (2007) analysed the Gaussian case and its convergence.
Exercise 2. Four blobs: two with standard deviation 0.5, 2.2 apart; one wide (1.2); one medium (0.8). 60 points each, 2 datasets, h on a log grid from 0.2 to 4.
| h | 0.20 | 0.35 | 0.45 | 0.59 | 0.78 | 1.03 | 1.35 | 1.77 | 2.32+ |
|---|---|---|---|---|---|---|---|---|---|
| modes | 48 | 13 | 7 | 5 | 4.5 | 3 | 3 | 3 | 1 |
| ARI | 0.51 | 0.80 | 0.91 | 0.96 | 0.96 | 0.70 | 0.69 | 0.66 | 0.00 |
- The best bandwidth reached ARI 0.964.
- The longest stable plateau was k = 3 (h from 1.03 to 1.77, ARI 0.69): the two close blobs merged.
- scikit-learn-style estimate_bandwidth (quantile 0.3) gave h = 3.43: one mode, ARI 0. Silverman’s rule gave h = 1.06: 3 modes, ARI 0.70.
- Automatic choices fail when clusters differ in scale and separation. Inspect the cluster count over a range of h, and use domain knowledge of the scale that matters.
Mean shift in AI, robotics and aerospace
- Visual tracking: mean-shift and CAMShift trackers follow a target’s colour histogram from frame to frame on drones and robots.
- Image and terrain segmentation: clustering pixels in joint colour–position space.
- Mode finding: dominant operating points of engines or actuators from telemetry, without fixing their number.
References
- K. Fukunaga, L. Hostetler (1975). “The estimation of the gradient of a density function, with applications in pattern recognition.” IEEE Transactions on Information Theory 21(1).
- Y. Cheng (1995). “Mean shift, mode seeking, and clustering.” IEEE TPAMI 17(8).
- D. Comaniciu, P. Meer (2002). “Mean shift: a robust approach toward feature space analysis.” IEEE TPAMI 24(5).
- M. Á. Carreira-Perpiñán (2007). “Gaussian mean-shift is an EM algorithm.” IEEE TPAMI 29(5).
- D. Comaniciu, V. Ramesh, P. Meer (2003). “Kernel-based object tracking.” IEEE TPAMI 25(5).