-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKFold_Result.py
89 lines (75 loc) · 2.68 KB
/
KFold_Result.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
import os
# Select dataset through task_id
task = ['UFFT', 'MA']
task_id = 0
print(task[task_id])
if task_id == 0:
# UFFT
all_sub = 30
elif task_id == 1:
# MA
all_sub = 29
all_acc = []
all_pre = []
all_rec = []
all_f1 = []
all_kap = []
for n_sub in range(1, all_sub + 1):
sub_acc = []
sub_pre = []
sub_rec = []
sub_f1 = []
sub_kap = []
for tr in range(5):
path = os.path.join('save', task[task_id], 'KFold', str(n_sub), str(tr+1))
val_acc = open(path + '/metrics.txt', "r")
string = val_acc.readlines()[-1]
acc = string.split('acc=')[1].split(', pre')[0]
pre = string.split('pre=')[1].split(', rec')[0]
rec = string.split('rec=')[1].split(', f1')[0]
f1 = string.split('f1=')[1].split(', kap')[0]
kappa = string.split('kap=')[1]
acc = float(acc)
pre = float(pre)
rec = float(rec)
f1 = float(f1) * 100
kappa = float(kappa)
sub_acc.append(acc)
sub_pre.append(pre)
sub_rec.append(rec)
sub_f1.append(f1)
sub_kap.append(kappa)
# old method for counting std (used in our paper). Recommended to use updated method.
# all_acc.append(acc)
# all_pre.append(pre)
# all_rec.append(rec)
# all_f1.append(f1)
# all_kap.append(kappa)
sub_acc = np.array(sub_acc)
sub_pre = np.array(sub_pre)
sub_rec = np.array(sub_rec)
sub_f1 = np.array(sub_f1)
sub_kap = np.array(sub_kap)
# updated method for counting std
all_acc.append(np.mean(sub_acc))
all_pre.append(np.mean(sub_pre))
all_rec.append(np.mean(sub_rec))
all_f1.append(np.mean(sub_f1))
all_kap.append(np.mean(sub_kap))
print('\nsub = %d : acc = %.2f ± %.2f' % (n_sub, np.mean(sub_acc), np.std(sub_acc)))
print('sub = %d : pre = %.2f ± %.2f' % (n_sub, np.mean(sub_pre), np.std(sub_pre)))
print('sub = %d : rec = %.2f ± %.2f' % (n_sub, np.mean(sub_rec), np.std(sub_rec)))
print('sub = %d : f1 = %.2f ± %.2f' % (n_sub, np.mean(sub_f1), np.std(sub_f1)))
print('sub = %d : kap = %.2f ± %.2f' % (n_sub, np.mean(sub_kap), np.std(sub_kap)))
print('\n=======> KFold-CV results of all subjects on ' + task[task_id])
all_acc = np.array(all_acc)
all_pre = np.array(all_pre)
all_rec = np.array(all_rec)
all_f1 = np.array(all_f1)
all_kap = np.array(all_kap)
print('acc = %.2f ± %.2f' % (np.mean(all_acc), np.std(all_acc)))
print('pre = %.2f ± %.2f' % (np.mean(all_pre), np.std(all_pre)))
print('rec = %.2f ± %.2f' % (np.mean(all_rec), np.std(all_rec)))
print('f1 = %.2f ± %.2f' % (np.mean(all_f1), np.std(all_f1)))
print('kap = %.2f ± %.2f' % (np.mean(all_kap), np.std(all_kap)))