Skip to content

Clustering, density estimation, and anomaly detection · Density and exemplar clustering · UCL025 · Algorithm

Mean shift

Locate modes of a kernel-smoothed distribution by repeatedly shifting candidate points toward local weighted means. Clustering follows by consolidating converged modes and assigning observations to them.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.meanshift. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Locate modes of a kernel-smoothed distribution by repeatedly shifting candidate points toward local weighted means. Clustering follows by consolidating converged modes and assigning observations to them.

Question / hypotheses

Which dense groups, modes, or exemplars persist under a defensible similarity scale? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

Repeated local weighted-mean updates followed by mode consolidation.

Assumptions

No fixed KK is required, but bandwidth implicitly controls effective complexity. Mode finding can be costly and high-dimensional KDE is fragile. A density mode need not correspond to a scientifically meaningful class.

Formula / computational definition

For Gaussian weights, xnew=∑iwi(x)xi/∑iwi(x)x^{new}=\sum_i w_i(x)x_i/\sum_iw_i(x) with wi(x)=exp⁡[−∥x−xi∥2/(2h2)]w_i(x)=\exp[-\|x-x_i\|^2/(2h^2)]. The displacement is related to the normalized gradient of the corresponding KDE. Other kernels produce different update details; scikit-learn's implementation uses a flat neighborhood kernel.

Parameters and notation

Learned or fitted quantities: Converged modes and observation-to-mode assignments. User-set hyperparameters/design choices: Bandwidth hh, seed placement, mode-merging threshold, and iteration cap control results. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable. Refit or rebuild neighborhoods when the reference distribution changes. Recalibrate neighborhood and extraction settings using stable density structure; verify whether the implementation supports genuine new-point assignment before deployment.

How to use and calibrate

Examine mode count versus bandwidth, convergence, tiny clusters, and seed sensitivity. Record the exact kernel when comparing implementations or formulas.

Example application

Find dominant colors or location modes without specifying their count. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. A stable partition can reflect nuisance structure such as batch, collection site, or missingness. Confirm the interpretation in original variables and an independent sample.

Python and R code examples

Use sklearn.cluster.MeanShift for its documented flat-kernel algorithm. A custom Gaussian-weighted iteration is straightforward in NumPy or R; label it Gaussian mean shift rather than claiming exact equivalence to the library's flat-kernel output.

Component: One Gaussian mean-shift step. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — One Gaussian mean-shift step. · needs numpy

import numpy as np
x=np.array([-1.,0.,1.]); q=.3; h=.7
w=np.exp(-.5*((q-x)/h)**2); result=np.sum(w*x)/w.sum()
print(result)
R

Component demo, not a trained model — One Gaussian mean-shift step.

x=c(-1,0,1); q=.3; h=.7; w=exp(-.5*((q-x)/h)^2)
result=sum(w*x)/sum(w)
print(result)

Sources

From the CodeSplash reference, entry UCL025.