标题聚类中常用评价标准
评价标准的介绍:
- ACC
- AMI
- ARI
- 轮廓系数
一、ACC准确度
用来计算聚类预测的准确性 acc= 预测个数/整个样本的个数
n:数据的个数
l
i
l_i
li: i的原始类别
r
i
r_i
ri: i预测的聚类标签
map(
r
i
r_i
ri): 为预测标签到原始类别的映射
δ
\delta
δ(x,y): δ(x,y)定义了一个delta函数,当x = y时等于1,否则等于0。
n:数据集的个数
由于我在python中没有直接找到计算ACC的函数,一下附上自己写的方法:
from __future__ import division
import numpy as np
from munkres import Munkres,print_matrix
from sklearn import metrics
#该方法用来做从预测标签到原始类别的映射,即map(ri)
#input:ri,li
def best_map(L1,L2):
#L1 should be the labels and L2 should be the clustering number we got
Label1 = np.unique(L1) # 去除重复的元素,由小大大排列
nClass1 = len(Label1) # 标签的大小
Label2 = np.unique(L2)
nClass2 = len(Label2)
nClass = np.maximum(nClass1,nClass2)
G = np.zeros((nClass,nClass))
for i in range(nClass1):
ind_cla1 = L1 == Label1[i]
ind_cla1 = ind_cla1.astype(float)
for j in range(nClass2):
ind_cla2 = L2 == Label2[j]
ind_cla2 = ind_cla2.astype(float)
G[i,j] = np.sum(ind_cla2 * ind_cla1)
m = Munkres()
index = m.compute(-G.T)
index = np.array(index)
c = index[:,1]
newL2 = np.zeros(L2.shape)
for i in range(nClass2):
newL2[L2 == Label2[i]] = Label1[c[i]]
return newL2.astype(int)
labels_true = [0, 0, 1, 1, 2, 2]
labels_pred = [1, 1, 2, 2, 0, 0]
from sklearn.metrics import accuracy_score
print('acc',accuracy_score(labels_true,labels_pred))
二、ARI(Adjusted Rand index )调整德兰指数
其范围在[-1,1],其值越大,预测值越大,则预测值与真实值越吻合
from sklearn import metrics
labels_true = [0, 0, 0, 1, 1, 1]
labels_pred = [0, 0, 1, 1, 2, 2]
print(metrics.adjusted_rand_score(labels_true, labels_pred) )
结果:
0.24
三、AMI
其范围在[-1,1],其值越大,预测值越大,则预测值与真实值越吻合
from sklearn import metrics
labels_true = [0, 0, 0, 1, 1, 1]
labels_pred = [0, 0, 1, 1, 2, 2]
print(metrics.adjusted_mutual_info_score(labels_true, labels_pred) )
结果:
0.22504
四、轮廓系数
from sklearn.metrics import silhouette_score as SI
##data为数据 pred为预测标签
SI(data, pred)