2010-07-27 06:50:50 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
=================================================
|
2010-07-27 06:50:50 +08:00
|
|
|
Demo of affinity propagation clustering algorithm
|
2010-11-02 18:38:06 +08:00
|
|
|
=================================================
|
2010-07-10 02:19:46 +08:00
|
|
|
|
|
|
|
|
Reference:
|
|
|
|
|
Brendan J. Frey and Delbert Dueck, "Clustering by Passing Messages
|
|
|
|
|
Between Data Points", Science Feb. 2007
|
|
|
|
|
|
|
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.cluster import AffinityPropagation
|
|
|
|
|
from sklearn import metrics
|
2019-10-28 05:17:23 +08:00
|
|
|
from sklearn.datasets import make_blobs
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-17 01:02:23 +08:00
|
|
|
# Generate sample data
|
2011-05-19 10:14:35 +08:00
|
|
|
centers = [[1, 1], [-1, -1], [1, -1]]
|
2012-04-28 20:04:39 +08:00
|
|
|
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5,
|
2012-12-25 20:16:05 +08:00
|
|
|
random_state=0)
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-17 01:02:23 +08:00
|
|
|
# Compute Affinity Propagation
|
2012-09-04 04:22:10 +08:00
|
|
|
af = AffinityPropagation(preference=-50).fit(X)
|
2010-08-19 17:42:14 +08:00
|
|
|
cluster_centers_indices = af.cluster_centers_indices_
|
|
|
|
|
labels = af.labels_
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2010-07-19 02:38:51 +08:00
|
|
|
n_clusters_ = len(cluster_centers_indices)
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print('Estimated number of clusters: %d' % n_clusters_)
|
|
|
|
|
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
|
|
|
|
|
print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
|
|
|
|
|
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
|
|
|
|
|
print("Adjusted Rand Index: %0.3f"
|
|
|
|
|
% metrics.adjusted_rand_score(labels_true, labels))
|
|
|
|
|
print("Adjusted Mutual Information: %0.3f"
|
2019-05-09 08:35:32 +08:00
|
|
|
% metrics.adjusted_mutual_info_score(labels_true, labels))
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Silhouette Coefficient: %0.3f"
|
|
|
|
|
% metrics.silhouette_score(X, labels, metric='sqeuclidean'))
|
2011-05-08 16:43:14 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-17 01:02:23 +08:00
|
|
|
# Plot result
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2010-07-10 02:19:46 +08:00
|
|
|
from itertools import cycle
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.close('all')
|
|
|
|
|
plt.figure(1)
|
|
|
|
|
plt.clf()
|
2010-07-10 02:19:46 +08:00
|
|
|
|
|
|
|
|
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
|
2010-07-19 02:38:51 +08:00
|
|
|
for k, col in zip(range(n_clusters_), colors):
|
2010-07-10 02:19:46 +08:00
|
|
|
class_members = labels == k
|
2010-07-19 02:38:51 +08:00
|
|
|
cluster_center = X[cluster_centers_indices[k]]
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(X[class_members, 0], X[class_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)
|
2010-07-10 02:19:46 +08:00
|
|
|
for x in X[class_members]:
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title('Estimated number of clusters: %d' % n_clusters_)
|
|
|
|
|
plt.show()
|