用Sklearn实现Affinity Propagation Clustering (AP聚类)

调用sklearn.cluster.AffinityPropagation即可实现AP聚类，不仅能接受原始数据作为输入，也可以预先计算好相似度矩阵，然后再送入。

原始数据输入

from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs

# Fake Data Generization
centers = [[1, 1], [-1, -1], [1, -1], [-1,1]]
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5,
random_state=0)

# Compute Affinity Propagation
af = AffinityPropagation(preference=-50).fit(X)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_

n_clusters_ = len(cluster_centers_indices)

# Plot result
import matplotlib.pyplot as plt
from itertools import cycle

plt.close('all')
plt.figure(1)
plt.clf()

colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.savefig("AP_feed_data.png")

接受相似度矩阵输入

相似度，顾名思义，数值越大表示相似性越强。一般我们会用欧氏距离，不开平方，且取负数。

from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
import numpy as np

# Fake Data Generization
centers = [[1, 1], [-1, -1], [1, -1], [-1,1]]
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5,
random_state=0)

# Similarity Compution
Similarity = np.zeros((300, 300))
for i in range(300):
for j in range(i, 300):
Similarity[i][j] = - ((X[i][0] - X[j][0])**2 + (X[i][1] - X[j][1])**2)
Similarity[j][i] = Similarity[i][j]

# Compute Affinity Propagation
af = AffinityPropagation(preference=-50, affinity='precomputed').fit(Similarity)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_

n_clusters_ = len(cluster_centers_indices)

# Plot result
import matplotlib.pyplot as plt
from itertools import cycle

plt.close('all')
plt.figure(1)
plt.clf()

colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.savefig("AP_feed_similarity.png")


完整代码

https://github.com/SongDark/APclustering

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120