2010-07-27 06:50:50 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
=============================================
|
2010-07-27 06:50:50 +08:00
|
|
|
A demo of the mean-shift clustering algorithm
|
2010-11-02 18:38:06 +08:00
|
|
|
=============================================
|
2010-06-29 06:49:54 +08:00
|
|
|
|
|
|
|
|
Reference:
|
2011-08-06 16:52:06 +08:00
|
|
|
|
|
|
|
|
Dorin Comaniciu and Peter Meer, "Mean Shift: A robust approach toward
|
|
|
|
|
feature space analysis". IEEE Transactions on Pattern Analysis and
|
|
|
|
|
Machine Intelligence. 2002. pp. 603-619.
|
2010-06-29 06:49:54 +08:00
|
|
|
|
|
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-06-29 06:49:54 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.cluster import MeanShift, estimate_bandwidth
|
|
|
|
|
from sklearn.datasets.samples_generator import make_blobs
|
2010-06-29 06:49:54 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-27 06:50:50 +08:00
|
|
|
# Generate sample data
|
2011-05-19 10:16:09 +08:00
|
|
|
centers = [[1, 1], [-1, -1], [1, -1]]
|
2011-08-19 19:59:35 +08:00
|
|
|
X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)
|
2010-06-29 06:49:54 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-27 06:50:50 +08:00
|
|
|
# Compute clustering with MeanShift
|
2011-08-06 04:58:12 +08:00
|
|
|
|
2011-08-06 16:52:06 +08:00
|
|
|
# The following bandwidth can be automatically detected using
|
2011-08-25 09:32:39 +08:00
|
|
|
bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500)
|
2011-08-06 04:58:12 +08:00
|
|
|
|
2011-08-19 01:03:50 +08:00
|
|
|
ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
|
2010-08-19 17:42:14 +08:00
|
|
|
ms.fit(X)
|
|
|
|
|
labels = ms.labels_
|
|
|
|
|
cluster_centers = ms.cluster_centers_
|
2010-06-29 06:49:54 +08:00
|
|
|
|
|
|
|
|
labels_unique = np.unique(labels)
|
|
|
|
|
n_clusters_ = len(labels_unique)
|
|
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("number of estimated clusters : %d" % n_clusters_)
|
2010-06-29 06:49:54 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-27 06:50:50 +08:00
|
|
|
# Plot result
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2010-06-29 06:49:54 +08:00
|
|
|
from itertools import cycle
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(1)
|
|
|
|
|
plt.clf()
|
2010-06-29 06:49:54 +08:00
|
|
|
|
|
|
|
|
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
|
|
|
|
|
for k, col in zip(range(n_clusters_), colors):
|
|
|
|
|
my_members = labels == k
|
|
|
|
|
cluster_center = cluster_centers[k]
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(X[my_members, 0], X[my_members, 1], col + '.')
|
|
|
|
|
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
|
2014-05-15 10:35:13 +08:00
|
|
|
markeredgecolor='k', markersize=14)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title('Estimated number of clusters: %d' % n_clusters_)
|
|
|
|
|
plt.show()
|