-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_threshold.py
More file actions
168 lines (129 loc) · 5.07 KB
/
binary_threshold.py
File metadata and controls
168 lines (129 loc) · 5.07 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import warnings
import numpy as np
import pandas as pd
from tqdm import tqdm
from itertools import product
from sklearn.metrics import f1_score
from module.argument import get_parser
from module.read_data import (
load_data,
multiclass2binary
)
from module.get_model import (
load_model
)
from module.common import (
data_split,
load_val_result,
print_best_param
)
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 300
warnings.filterwarnings('ignore')
def main():
tg_nums = [403, 412, 413]
inhale_types = ['vapour', 'aerosol']
model_names = ['logistic', 'lda', 'qda', 'plsda', 'dt', 'rf', 'gbt', 'xgb', 'lgb', 'mlp']
thresholds = np.round(np.arange(0.1, 1, 0.1), 1).tolist()
metric = 'f1'
comb = list(product(tg_nums, inhale_types, model_names, thresholds))
# data_comb = list(product(tg_nums, inhale_types))
f1_dict = {x: {y: {z: {}.fromkeys(thresholds) for z in model_names} for y in inhale_types} for x in tg_nums}
for c in tqdm(comb):
tg_num, inhale_type, model_name, thres = c
x, y = load_data(path = 'data', args = args)
y = multiclass2binary(y, tg_num)
result = load_val_result('', tg_num, inhale_type, model_name)
best_param = print_best_param(val_result = result, metric = metric)
# test reulst
f1 = []
for seed in range(10):
x_train, x_test, y_train, y_test = data_split(x, y, seed)
model = load_model(model = model_name, seed = seed, param = best_param)
model.fit(x_train, y_train)
if model_name == 'plsda':
pred_score = model.predict(x_test)
else:
pred_score = model.predict_proba(x_test)[:, 1]
pred = np.where(pred_score < thres, 0, 1).reshape(-1)
f1.append(f1_score(y_test, pred))
f1_dict[tg_num][inhale_type][model_name][thres] = {'mean': np.mean(f1), 'std': np.std(f1)}
return f1_dict
f1_dict = main()
model_names = ['logistic', 'lda', 'qda', 'plsda', 'dt', 'rf', 'gbt', 'xgb', 'lgb', 'mlp']
#
vapor403 = pd.DataFrame(f1_dict[403]['vapour'])
vapor403 = pd.concat([vapor403[x].apply(pd.Series) for x in list(vapor403)], 1, keys=list(vapor403))
plt.figure(figsize = (10, 5))
for m in model_names:
plt.plot(vapor403.index, vapor403[m]['mean'], '-o', markersize='3')
# plt.errorbar(vapor403.index, vapor403[m]['mean'], yerr = vapor403[m]['std'], capsize = 4, fmt = '-o', markersize='3')
plt.ylim((0, 1))
plt.legend(model_names, ncol = 2)
plt.title('Actue Vapor F1 score')
plt.show()
plt.close()
m = 'logistic'
plt.figure(figsize = (10, 5))
plt.errorbar(vapor403.index, vapor403[m]['mean'], yerr = vapor403[m]['std'], capsize = 4, fmt = '-o', markersize='3')
plt.ylim((0, 1))
plt.title('Actue Vapor Logistic Regression F1 score')
plt.show()
plt.close()
#
aerosol403 = pd.DataFrame(f1_dict[403]['aerosol'])
aerosol403 = pd.concat([aerosol403[x].apply(pd.Series) for x in list(aerosol403)], 1, keys = list(aerosol403))
plt.figure(figsize = (10, 5))
for m in model_names:
plt.plot(aerosol403.index, aerosol403[m]['mean'], '-o', markersize='3')
# plt.errorbar(vapor403.index, vapor403[m]['mean'], yerr = vapor403[m]['std'], capsize = 4, fmt = '-o', markersize='3')
plt.ylim((0, 1))
plt.legend(model_names, ncol = 2)
plt.title('Actue Aerosol F1 score')
plt.show()
plt.close()
#
vapor412 = pd.DataFrame(f1_dict[412]['vapour'])
vapor412 = pd.concat([vapor412[x].apply(pd.Series) for x in list(vapor412)], 1, keys = list(vapor412))
plt.figure(figsize = (10, 5))
for m in model_names:
plt.plot(vapor412.index, vapor412[m]['mean'], '-o', markersize='3')
plt.ylim((0, 1))
plt.legend(model_names, ncol = 2)
plt.title('Sub-Actue Vapor F1 score')
plt.show()
plt.close()
#
aerosol412 = pd.DataFrame(f1_dict[412]['aerosol'])
aerosol412 = pd.concat([aerosol412[x].apply(pd.Series) for x in list(aerosol412)], 1, keys = list(aerosol412))
plt.figure(figsize = (10, 5))
for m in model_names:
plt.plot(aerosol412.index, aerosol412[m]['mean'], '-o', markersize='3')
# plt.errorbar(aerosol412.index, aerosol412[m]['mean'], yerr = aerosol412[m]['std'], capsize = 4, fmt = '-o', markersize='3')
plt.ylim((0, 1))
plt.legend(model_names, ncol = 2)
plt.title('Sub-Actue Aerosol F1 score')
plt.show()
plt.close()
#
vapor413 = pd.DataFrame(f1_dict[413]['vapour'])
vapor413 = pd.concat([vapor413[x].apply(pd.Series) for x in list(vapor413)], 1, keys = list(vapor413))
plt.figure(figsize = (10, 5))
for m in model_names:
plt.plot(vapor413.index, vapor413[m]['mean'], '-o', markersize='3')
plt.ylim((0, 1))
plt.legend(model_names, ncol = 2)
plt.title('Sub-Chronic Vapor F1 score')
plt.show()
plt.close()
#
aerosol413 = pd.DataFrame(f1_dict[413]['aerosol'])
aerosol413 = pd.concat([aerosol413[x].apply(pd.Series) for x in list(aerosol413)], 1, keys = list(aerosol413))
plt.figure(figsize = (10, 5))
for m in model_names:
plt.plot(aerosol413.index, aerosol413[m]['mean'], '-o', markersize='3')
plt.ylim((0, 1))
plt.legend(model_names, ncol = 2)
plt.title('Sub-Chronic Aerosol F1 score')
plt.show()
plt.close()