-
Notifications
You must be signed in to change notification settings - Fork 10
/
metrics.py
65 lines (49 loc) · 1.99 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
from scipy.optimize import linear_sum_assignment
from sklearn.metrics import adjusted_rand_score, accuracy_score, fowlkes_mallows_score, normalized_mutual_info_score, adjusted_mutual_info_score
def nmi_score(y, y_pred):
return normalized_mutual_info_score(y, y_pred)
def ami_score(y, y_pred):
return adjusted_mutual_info_score(y, y_pred)
def ari_score(y, y_pred):
return adjusted_rand_score(y, y_pred)
def fms_score(y, y_pred):
return fowlkes_mallows_score(y, y_pred)
def cluster_acc(y_true, y_pred):
"""
Calculate unsupervised clustering accuracy. Requires scikit-learn installed
# Arguments
y_true: 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
row_ind, col_ind = linear_sum_assignment(w.max() - w)
return w[row_ind, col_ind].sum() * 1.0 / y_pred.size
def cluster_purity(y_true, y_pred):
"""
Calculate clustering purity
https://en.wikipedia.org/wiki/Cluster_analysis#Evaluation_and_assessment
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
purity, 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
label_mapping = w.argmax(axis=1)
y_pred_voted = y_pred.copy()
for i in range(y_pred.size):
y_pred_voted[i] = label_mapping[y_pred[i]]
return accuracy_score(y_pred_voted, y_true)