【发布时间】:2020-01-26 04:33:39
【问题描述】:
我正在尝试将 sklearn 的相似性传播实现用于相当简单的集群,但是,我得到了一些奇怪的结果。我试图使用 300 个集群(每个集群 3 个点)的 AP,但它失败了,所以我尝试了一个看似简单的集群问题,即 5 个高斯分布式集群,每个集群 100 个点。生成的图表链接如下。有谁知道我哪里出错了?
我关注了@Anony-Mousse 对this 的回复,但是,增加阻尼和最大迭代并没有真正的帮助。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AffinityPropagation
from itertools import cycle
n_per_cluster = 100
n_clusters = 5
n_total = n_per_cluster*n_clusters
x = np.empty(n_total)
y = np.empty(n_total)
labels = np.empty(n_total)
count = 0
for i in range(n_clusters):
xseed = np.random.random()*100
yseed = np.random.random()*100
normX = np.random.normal(xseed,1,n_per_cluster)
normY = np.random.normal(yseed,1,n_per_cluster)
normCount = 0
for j in range(n_per_cluster):
x[count] = normX[normCount]
y[count] = normY[normCount]
labels[count] = i
normCount+=1
count+=1
#print(labels)
#print(x, y)
# plt.scatter(x,y)
# plt.show()
preference = -50
max_iter = 1000
xy = np.column_stack((x,y))
af = AffinityPropagation(damping = 0.9, preference = preference, verbose = True, max_iter = max_iter).fit(xy)
_exemplars_index = af.cluster_centers_indices_
_labels = af.labels_
_n_cluster = len(_exemplars_index)
plt.close('all')
plt.figure(1)
plt.clf()
colors = cycle('bgrcmyk')
for k,col in zip(range(_n_cluster),colors):
class_members = labels == k #error check
exemplars = xy[_exemplars_index[k]]
plt.plot(xy[class_members, 0], xy[class_members,1], col + '.')
plt.plot(exemplars[0], exemplars[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in xy[class_members]:
plt.plot([exemplars[0], x[0]], [exemplars[1], x[1]], col)
plt.title('Estimated number of clusters: %d' % _n_cluster)
plt.show()
集群是正确的,但样本在屏幕上。这是一个非常直接的聚类问题,所以我想这是用户错误,但我还没有弄清楚。感谢您的帮助
【问题讨论】:
标签: python scikit-learn cluster-analysis