Density-based exploratory clustering
You have unlabeled vector data whose groups correspond to dense regions and do not want to choose the number of clusters first.
Scikit-Learn · Class
MeanShift performs unsupervised clustering with a flat kernel, automatically discovering cluster centers from sample density. It updates candidate centroids toward local means and removes near-duplicate candidates.
MeanShift starts kernel candidates from seeds or a binning scheme, repeatedly moves each candidate toward the mean of nearby samples, then removes nearly duplicate candidates. The surviving centroids define the clusters, while labels associate samples with them; the number of clusters is determined by the data rather than supplied directly.
class sklearn.cluster.MeanShift(*, bandwidth=None, seeds=None, bin_seeding=False, min_bin_freq=1, cluster_all=True, n_jobs=None, max_iter=300)| Name | Type | Description |
|---|---|---|
bandwidth | float, default=None | Flat-kernel bandwidth; when omitted, it is estimated with estimate_bandwidth. |
seeds | array-like of shape (n_samples, n_features), default=None | Kernel initialization points. |
bin_seeding | bool, default=False | Whether to use discretized, binned points as initial kernel locations. |
min_bin_freq | int, default=1 | Minimum number of points required in a bin when generating seed locations. |
cluster_all | bool, default=True | Whether every sample must receive a cluster assignment, including orphan points. |
max_iter | int, default=300 | Maximum number of iterations per seed. |
n_jobs | int, default=None | Number of parallel jobs; None means the default job behavior. |
Fitted estimator with cluster_centers_ containing the coordinates of cluster centers and labels_ containing the label of each sample.
from sklearn.cluster import MeanShift
X = [[1, 1], [1, 0], [0, 1], [8, 8], [8, 9], [9, 8]]
model = MeanShift(bandwidth=2).fit(X)
print(model.labels_)
print(model.cluster_centers_)
Fits MeanShift to two separated groups and prints the assigned labels and learned centroids.
You have unlabeled vector data whose groups correspond to dense regions and do not want to choose the number of clusters first.
You want to reduce many nearby candidate centers to representative locations after mean-shift updates.
A large sample set makes one kernel start per observation costly.
MeanShift is an unsupervised, centroid-based clustering algorithm that uses a flat kernel. It updates candidate centroids toward the mean of points in a neighborhood, then filters near-duplicate candidates.
Follow-up: How is the number of clusters selected? It is induced by the surviving candidate centroids after iterative updates and post-processing, rather than specified as a separate cluster-count parameter.
bandwidth controls the size of the neighborhood used by the flat kernel. If omitted, scikit-learn estimates it with estimate_bandwidth.
Follow-up: What trade-off does bandwidth introduce? A larger neighborhood generally merges broader regions, while a smaller one can preserve finer structure; it also changes the computational workload.
Seeds initialize the kernels. If seeds are absent, scikit-learn derives them with get_bin_seeds, using bandwidth as the grid size and default values for the other options.
Follow-up: How can initialization improve scalability? Use fewer seeds, for example by increasing min_bin_freq in the bin-seed calculation.
sklearn.cluster.estimate_bandwidthsklearn.cluster.get_bin_seedssklearn.cluster.mean_shiftsklearn.cluster.KMeanssklearn.cluster.DBSCAN