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
|
|
|
|
|
|
|
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
print __doc__
|
2010-07-10 02:19:46 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.cluster import AffinityPropagation
|
|
|
|
|
from sklearn import metrics
|
|
|
|
|
from sklearn.datasets.samples_generator import make_blobs
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2011-05-08 16:43:14 +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]]
|
|
|
|
|
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5)
|
2011-05-08 16:43:14 +08:00
|
|
|
|
|
|
|
|
##############################################################################
|
2010-07-17 01:02:23 +08:00
|
|
|
# Compute similarities
|
2011-05-15 21:24:22 +08:00
|
|
|
X_norms = np.sum(X ** 2, axis=1)
|
2011-05-08 16:43:14 +08:00
|
|
|
S = - X_norms[:, np.newaxis] - X_norms[np.newaxis, :] + 2 * np.dot(X, X.T)
|
|
|
|
|
p = 10 * np.median(S)
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2011-05-08 16:43:14 +08:00
|
|
|
##############################################################################
|
2010-07-17 01:02:23 +08:00
|
|
|
# Compute Affinity Propagation
|
2011-05-08 16:43:14 +08:00
|
|
|
af = AffinityPropagation().fit(S, p)
|
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
|
|
|
|
|
|
|
|
print 'Estimated number of clusters: %d' % n_clusters_
|
2011-05-15 21:24:22 +08:00
|
|
|
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)
|
2011-09-13 22:50:30 +08:00
|
|
|
print "Adjusted Rand Index: %0.3f" % \
|
|
|
|
|
metrics.adjusted_rand_score(labels_true, labels)
|
2011-10-21 07:52:42 +08:00
|
|
|
print "Adjusted Mutual Information: %0.3f" % \
|
2011-11-07 19:46:11 +08:00
|
|
|
metrics.adjusted_mutual_info_score(labels_true, labels)
|
2011-10-04 07:50:55 +08:00
|
|
|
D = (S / np.min(S))
|
2011-10-04 08:23:14 +08:00
|
|
|
print ("Silhouette Coefficient: %0.3f" %
|
|
|
|
|
metrics.silhouette_score(D, labels, metric='precomputed'))
|
2011-05-08 16:43:14 +08:00
|
|
|
|
|
|
|
|
##############################################################################
|
2010-07-17 01:02:23 +08:00
|
|
|
# Plot result
|
2010-07-10 02:19:46 +08:00
|
|
|
import pylab as pl
|
|
|
|
|
from itertools import cycle
|
|
|
|
|
|
|
|
|
|
pl.close('all')
|
|
|
|
|
pl.figure(1)
|
|
|
|
|
pl.clf()
|
|
|
|
|
|
|
|
|
|
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]]
|
2011-05-08 16:43:14 +08:00
|
|
|
pl.plot(X[class_members, 0], X[class_members, 1], col + '.')
|
2010-07-10 02:19:46 +08:00
|
|
|
pl.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
|
2011-05-08 16:43:14 +08:00
|
|
|
markeredgecolor='k', markersize=14)
|
2010-07-10 02:19:46 +08:00
|
|
|
for x in X[class_members]:
|
|
|
|
|
pl.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
|
|
|
|
|
|
|
|
|
|
pl.title('Estimated number of clusters: %d' % n_clusters_)
|
2010-07-31 21:27:54 +08:00
|
|
|
pl.show()
|