【发布时间】:2019-07-21 18:44:21
【问题描述】:
我想写一个python代码来计算集群准确率r如下:
r = (A1+ ... +Ai+ ...Ak) / (the number of data objects)
其中 Ai 是第 i 个集群及其对应的真实集群中出现的数据对象的数量。
我需要实现它,以便将聚类性能与使用此准确性标准的研究论文进行比较。
我在sklearn中搜索了现有的方法,但找不到这样做的方法,并尝试自己编写。
这是我写的代码:
# For each label in prediction, extract true labels of the same
# index as 'labels'. Then count the number of instances of respective
# true labels in 'labels', and assume the one with the maximum
# number of instances is the corresponding true label.
pred_to_true_conversion={}
for p in np.unique(pred):
labels=true[pred==p]
unique, counts=np.unique(labels, return_counts=True)
label_count=dict(zip(unique, counts))
pred_to_true_conversion[p]=max(label_count, key=label_count.get)
# count the number of instances whose true label is the same
# as the converted predicted label.
count=0
for t, p in zip(true, pred):
if t==pred_to_true_conversion[p]: count+=1
return count/len(true)
但是,我不认为我的“标签重映射”方法是一种聪明的方法,应该有更好的方法来计算r。我的方法有如下问题:
- 它依赖于一个假设,即对应的真实标签是预测集群中出现频率最高的标签,但并非总是如此。
- 不同的预测聚类标签与相同的真实聚类标签相关,尤其是当真实标签和预测标签中的类数不同时。
如何实现准确度r?或者任何现有的集群库中是否有方法可以做到这一点?
【问题讨论】: