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
|
2010-08-27 15:29:29 +08:00
|
|
|
from scikits.learn.cluster import AffinityPropagation
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2010-07-17 01:02:23 +08:00
|
|
|
################################################################################
|
|
|
|
|
# Generate sample data
|
|
|
|
|
################################################################################
|
2010-07-10 02:19:46 +08:00
|
|
|
np.random.seed(0)
|
|
|
|
|
|
|
|
|
|
n_points_per_cluster = 100
|
|
|
|
|
n_clusters = 3
|
|
|
|
|
n_points = n_points_per_cluster*n_clusters
|
|
|
|
|
means = np.array([[1,1],[-1,-1],[1,-1]])
|
|
|
|
|
std = .5
|
|
|
|
|
|
|
|
|
|
X = np.empty((0, 2))
|
|
|
|
|
for i in range(n_clusters):
|
|
|
|
|
X = np.r_[X, means[i] + std * np.random.randn(n_points_per_cluster, 2)]
|
|
|
|
|
|
2010-07-17 01:02:23 +08:00
|
|
|
################################################################################
|
|
|
|
|
# Compute similarities
|
|
|
|
|
################################################################################
|
2010-07-10 02:19:46 +08:00
|
|
|
X_norms = np.sum(X*X, axis=1)
|
|
|
|
|
S = - X_norms[:,np.newaxis] - X_norms[np.newaxis,:] + 2 * np.dot(X, X.T)
|
|
|
|
|
p = 10*np.median(S)
|
|
|
|
|
|
2010-07-17 01:02:23 +08:00
|
|
|
################################################################################
|
|
|
|
|
# Compute Affinity Propagation
|
|
|
|
|
################################################################################
|
2010-07-10 02:19:46 +08:00
|
|
|
|
2010-08-19 17:42:14 +08:00
|
|
|
af = AffinityPropagation()
|
|
|
|
|
af.fit(S, p)
|
|
|
|
|
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_
|
|
|
|
|
|
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]]
|
2010-07-10 02:19:46 +08:00
|
|
|
pl.plot(X[class_members,0], X[class_members,1], col+'.')
|
|
|
|
|
pl.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
|
|
|
|
|
markeredgecolor='k', markersize=14)
|
|
|
|
|
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()
|
|
|
|
|
|