python - Sklearn 的 Affinity Propagation,好依赖坏样本
问题描述
我正在尝试将 sklearn 的亲和力传播实现用于相当简单的集群,但是,我得到了一些时髦的结果。我试图使用 300 个集群(每个集群 3 个点)的 AP,但它失败了,所以我尝试了一个看似简单的集群问题,即 5 个高斯分布式集群,每个集群 100 个点。生成的图表链接如下。有谁知道我哪里出错了?
我关注了@Anony-Mousse对此的回应,但是,增加阻尼和最大迭代并没有真正帮助。
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()
它使集群正确,但示例在屏幕上。这是一个非常直接的聚类问题,所以我想这是用户错误,但我还没有弄清楚。谢谢您的帮助
解决方案
抱歉,我应该在发布之前尝试参数化扫描。显然,AP 对度量数据非常敏感。我在偏好 = -100、阻尼 =0.95 和 1500 次迭代时得到了不错的结果。
推荐阅读
- c# - 如何将带有错误规范(无效的用户名或无效的密码)的 HttpResponseException 返回到前端?
- php - 变量 $term_id 没有出现在 wordpress 的 get_term_meta 函数中
- javascript - 如何更新自引用对象文字中的动态属性?
- php - 使用 fetch(PDO::FETCH_ASSOC); 无法获得所有结果;
- sql - 使用 ActiveRecord 获取具有条件的一系列计数
- html - 为什么引导程序将默认字体大小设置为 10px?
- powershell - 为什么 Test-AzureName 总是返回 false?
- python - 没有为 http.cookiejar 找到匹配的分布
- couchdb - CouchDB/Cloudant 按键查询视图是数组吗?
- r - 如何按 id 应用 grep() 数据