Lesson 43
Mean Shift
Slide each point toward the densest nearby region until points that share a peak form a cluster. One example is on this page.
Intermediate12 min
What You Will Learn
- Idea
- Example
- When to use it
Idea
Mean shift does not ask for k. Each point steps toward the average of its neighbors inside a bandwidth. Points that climb to the same peak share a cluster. The bandwidth is the knob that decides how wide a peak is.
Example
import numpy as np
from sklearn.cluster import MeanShift
X = np.array([[0, 0], [0.2, 0.1], [5, 5], [5.2, 4.8], [5.1, 5.3]], dtype=float)
labels = MeanShift(bandwidth=1.0).fit_predict(X)
print(f"clusters: {len(set(labels.tolist()))}")
print(labels.tolist())Output:
clusters: 2
[1, 1, 0, 0, 0]When to use it
- Use it when you do not know the number of dense peaks.
- Bandwidth is the whole model. Too small splits one cloud. Too large merges them.
- It is heavier than k-means, so start on a sample.
Loading playground…