Implementation:Scikit learn Scikit learn MeanShift
| Knowledge Sources | |
|---|---|
| Domains | Clustering, Density-Based Clustering |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for performing mean shift clustering using a flat kernel provided by scikit-learn.
Description
MeanShift is a centroid-based clustering algorithm that discovers blobs in a smooth density of samples. It works by iterating over candidate centroids, updating each to be the mean of points within a given region (the bandwidth), and then filtering near-duplicates in a post-processing stage. Seeding of initial candidates is performed using a binning technique for scalability. Unlike K-Means, MeanShift does not require specifying the number of clusters; instead the number of clusters is determined by the bandwidth parameter and data distribution.
Usage
Use MeanShift when you do not know the number of clusters ahead of time and want to discover dense regions in continuous data. It is suitable for image processing, computer vision, and spatial data analysis. The bandwidth parameter controls resolution; the estimate_bandwidth utility function can help select it automatically.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/cluster/_mean_shift.py
Signature
class MeanShift(ClusterMixin, BaseEstimator):
def __init__(
self,
*,
bandwidth=None,
seeds=None,
bin_seeding=False,
min_bin_freq=1,
cluster_all=True,
n_jobs=None,
max_iter=300,
):
Import
from sklearn.cluster import MeanShift
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| bandwidth | float or None | No | Bandwidth used in the flat kernel. If None, estimated automatically using estimate_bandwidth. Default is None. |
| seeds | array-like or None | No | Seeds used to initialize kernels. If None, seeds are calculated using bin seeding. Default is None. |
| bin_seeding | bool | No | Whether to use binning to seed initial kernel locations for scalability. Default is False. |
| min_bin_freq | int | No | Minimum number of points in a bin to seed it as a kernel location. Default is 1. |
| cluster_all | bool | No | If True, all points are clustered including orphans. If False, orphans get label -1. Default is True. |
| n_jobs | int or None | No | Number of parallel jobs. Default is None. |
| max_iter | int | No | Maximum number of iterations per seed point. Default is 300. |
Outputs
| Name | Type | Description |
|---|---|---|
| cluster_centers_ | ndarray of shape (n_clusters, n_features) | Coordinates of cluster centers. |
| labels_ | ndarray of shape (n_samples,) | Cluster labels for each sample. |
| n_iter_ | int | Maximum number of iterations performed on any seed. |
Usage Examples
Basic Usage
from sklearn.cluster import MeanShift
import numpy as np
X = np.array([[1, 1], [2, 1], [1, 0],
[4, 7], [3, 5], [3, 6]])
clustering = MeanShift(bandwidth=2).fit(X)
print(clustering.labels_)
print(clustering.cluster_centers_)