-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetrics.py
39 lines (30 loc) · 924 Bytes
/
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
import numpy as np
from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score
nmi = normalized_mutual_info_score
ari = adjusted_rand_score
def eval_acc(tru, pre):
# true label: numpy, vector in col
# pred lable: numpy, vector in row
tru = np.array(tru)
num_labels = tru.shape[0]
# accuracy
tru = np.reshape(tru, (num_labels))
#set_tru = set(tru.tolist())
set_pre = set(pre.tolist())
#nclu_tru = len(set_tru) # in case that nclu_tru != the preset cluster num
nclu_pre = len(set_pre)
correct = 0
for i in range(nclu_pre):
flag = list(set_pre)[i]
idx = np.argwhere(pre == flag)
correct += max(np.bincount(tru[idx].T[0].astype(np.int64)))
acc = correct / num_labels
return acc
def mask_nmi(y_true, y_pred):
n_y_ture = []
n_y_pred = []
for i in range(len(y_true)):
if y_true[i]>=0:
n_y_ture.append(y_true[i])
n_y_pred.append(y_pred[i])
return nmi(n_y_ture,n_y_pred)