Spec-Zone.ru › scikit-learn

Примечание

Перейти к концу для загрузки полного кода примера или для запуска этого примера в вашем браузере через JupyterLite или Binder

Демонстрация алгоритма кластеризации методом средних сдвигов

Ссылка:

Dorin Comaniciu и Peter Meer, «Mean Shift: A robust approach toward feature space analysis». IEEE Transactions on Pattern Analysis and Machine Intelligence. 2002. стр. 603-619.

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import numpy as np

from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets import make_blobs

Генерация образцовых данных

centers = [[1, 1], [-1, -1], [1, -1]]
X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)

Вычисление кластеризации методом MeanShift

# The following bandwidth can be automatically detected using
bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500)

ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_

labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)

print("number of estimated clusters : %d" % n_clusters_)
number of estimated clusters : 3

Вывод результата

import matplotlib.pyplot as plt

plt.figure(1)
plt.clf()

colors = ["#dede00", "#377eb8", "#f781bf"]
markers = ["x", "o", "^"]

for k, col in zip(range(n_clusters_), colors):
    my_members = labels == k
    cluster_center = cluster_centers[k]
    plt.plot(X[my_members, 0], X[my_members, 1], markers[k], color=col)
    plt.plot(
        cluster_center[0],
        cluster_center[1],
        markers[k],
        markerfacecolor=col,
        markeredgecolor="k",
        markersize=14,
    )
plt.title("Estimated number of clusters: %d" % n_clusters_)
plt.show()
Estimated number of clusters: 3

Общее время выполнения скрипта: (0 минут 0,463 секунды)

Launch binder
Launch JupyterLite

Download Jupyter notebook: plot_mean_shift.ipynb

Download Python source code: plot_mean_shift.py

Download zipped: plot_mean_shift.zip

Связанные примеры

Демонстрация алгоритма кластеризации распространением сродства

Сравнение алгоритмов кластеризации K-Means и MiniBatchKMeans

Демонстрация алгоритма кластеризации DBSCAN

Пример инициализации K-Means++

© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/auto_examples/cluster/plot_mean_shift.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API