-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.py
executable file
·48 lines (39 loc) · 1.51 KB
/
eval.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
import string
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
import argparse
parser = argparse.ArgumentParser(description='main', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dataset', default='ai', choices=['ai', 'bio'])
args = parser.parse_args()
dataset = args.dataset
print('\n### Testing ###')
p = dict()
with open(f'{dataset}/label_hier.txt') as fin:
for line in fin:
data = line.strip().split()
for label in data[1:]:
p[label.lower()] = data[0].lower()
y_u = []
y_d = []
with open(f'{dataset}/labels.txt') as fin:
for line in fin:
dl = line.strip().lower()
y_u.append(p[dl])
y_d.append(dl)
y_u_pred = []
y_d_pred = []
with open(f'{dataset}/out.txt') as fin:
for line in fin:
data = line.strip().split()
y_u_pred.append(data[0].lower())
y_d_pred.append(data[1].lower())
print('Upper Micro F1 score:', f1_score(y_u, y_u_pred, average='micro'))
print('Upper Macro F1 score:', f1_score(y_u, y_u_pred, average='macro'))
print('Upper-level Confusion Matrix:')
print(confusion_matrix(y_u, y_u_pred))
print('Lower Micro F1 score:', f1_score(y_d, y_d_pred, average='micro'))
print('Lower Macro F1 score:', f1_score(y_d, y_d_pred, average='macro'))
print('Lower-level Confusion Matrix:')
print(confusion_matrix(y_d, y_d_pred))
print('Overall Micro F1 score:', f1_score(y_u+y_d, y_u_pred+y_d_pred, average='micro'))
print('Overall Macro F1 score:', f1_score(y_u+y_d, y_u_pred+y_d_pred, average='macro'))