# 介绍：

A C = ∑ i = 1 n δ ( s i , m a p ( r i ) ) N AC = \frac{\sum_{i=1}^{n}\delta(s_i , map(r_i))}{N}

δ ( x , y ) = { 1 i f x = y 0 o t h e r w i s e \delta(x,y) = \begin{cases} 1 & if x=y \\ 0 & otherwise \end{cases}

# Python代码：

import numpy as np

def acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape (n_samples,)
y_pred: predicted labels, numpy.array with shape (n_samples,)
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size

if __name__ == '__main__':
# A为真实值、B,C,D为等效果的预测值 即: func(A,B)=func(A,C); func(B,D)=func(C,D);
A = np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3])
B = np.array([1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 1, 1, 3, 3, 3])
C = np.array([2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 3, 2, 2, 3, 3, 3])  # 将B中的 1 与 2 互换
D = np.array([1, 3, 1, 1, 1, 1, 1, 3, 3, 3, 3, 2, 1, 1, 2, 2, 2])  # 将B中的 2 与 3 互换

# 测试
print(acc(A, B))  # 0.7058823529411765
print(acc(A, C))  # 0.7058823529411765
print(metrics.accuracy_score(A, B))  # 0.7058823529411765
print(acc(B, D))  # 1.0
print(acc(C, D))  # 1.0

07-25 2万+
11-17

09-03 2万+
02-09 1379
03-05 3万+
05-25 1万+
09-19 1万+