-
Notifications
You must be signed in to change notification settings - Fork 1
/
sklearn_functions.py
executable file
·380 lines (268 loc) · 12.5 KB
/
sklearn_functions.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# -*- coding: utf-8 -*-
from joblib import Parallel, delayed
import matplotlib.pyplot as plt
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, f1_score
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier as MLP
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
if __package__ is None or __package__ == '':
import data_source as ds
from params import Params, save_fig
import preprocessing as pp
else:
from . import data_source as ds
from .params import Params, save_fig
from . import preprocessing as pp
def sklearn_kfolds(P,V=None):
P.log(P)
F = pp.perform_preprocessing(P, ds.get_data(P,V), P.copy().set_keys( sample_no = None, undersampling = False, oversampling = False, ))
X, Y = F[0]
x_test, y_test = F[2]
skf = StratifiedKFold(n_splits=P.get('runs'),shuffle=True,random_state=42)
# ''' Multi-layer Perceptron '''
# res = np.empty(shape=(P.get('runs'),5))
# for run, (train_index, test_index) in enumerate(skf.split(X, Y)):
# x_train, y_train = X[test_index], Y[test_index].ravel()
# clf = MLP(hidden_layer_sizes=(100,100),max_iter=500)
# clf.fit(x_train, y_train)
# y_pred = clf.predict(x_train)
# res[run,0] = accuracy_score(y_train,y_pred)
# res[run,1] = f1_score(y_train,y_pred,average='macro')
# y_pred = clf.predict(x_test)
# res[run,2] = accuracy_score(y_test,y_pred)
# res[run,3] = f1_score(y_test,y_pred,average='macro')
# res[run,4] = clf.n_iter_
# res = np.mean(res,axis=0)
# P.log("")
# P.log(f"MLP Acc Train: {res[0]:.5f}")
# P.log(f"MLP F1 Train: {res[1]:.5f}")
# P.log("")
# P.log(f"MLP Acc Test: {res[2]:.5f}")
# P.log(f"MLP F1 Test: {res[3]:.5f}")
# P.log(F"MLP Iterations = {res[4]}")
''' AdaBoost '''
res = np.empty(shape=(P.get('runs'),4))
for run, (train_index, test_index) in enumerate(skf.split(X, Y)):
x_train, y_train = X[test_index], Y[test_index].ravel()
base = DecisionTreeClassifier(max_depth=10)
clf = AdaBoostClassifier(base_estimator=base, n_estimators=100, learning_rate=0.5, random_state=42)
clf.fit(x_train, y_train)
y_pred = np.round(clf.predict(x_train))
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = np.round(clf.predict(x_test))
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"Ada Acc Train: {res[0]:.5f}")
P.log(f"Ada F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"Ada Acc Test: {res[2]:.5f}")
P.log(f"Ada F1 Test: {res[3]:.5f}")
''' Random Forest Classifier '''
res = np.empty(shape=(P.get('runs'),4))
for run, (train_index, test_index) in enumerate(skf.split(X, Y)):
x_train, y_train = X[test_index], Y[test_index].ravel()
clf = RandomForestClassifier()
clf.fit(x_train, y_train)
y_pred = clf.predict(x_train)
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = clf.predict(x_test)
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"RFC Acc Train: {res[0]:.5f}")
P.log(f"RFC F1 Train: {res[1]:.5f}")
P.log("")
P.log(f"RFC Acc Test: {res[2]:.5f}")
P.log(f"RFC F1 Test: {res[3]:.5f}")
''' Gaussian Naive Bayes '''
res = np.empty(shape=(P.get('runs'),4))
for run, (train_index, test_index) in enumerate(skf.split(X, Y)):
x_train, y_train = X[test_index], Y[test_index].ravel()
clf = GaussianNB()
clf.fit(x_train, y_train)
y_pred = clf.predict(x_train)
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = clf.predict(x_test)
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"GNB Acc Train: {res[0]:.5f}")
P.log(f"GNB F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"GNB Acc Test: {res[2]:.5f}")
P.log(f"GNB F1 Test: {res[3]:.5f}")
''' Support Vector Classification '''
res = np.empty(shape=(P.get('runs'),4))
for run, (train_index, test_index) in enumerate(skf.split(X, Y)):
print(f"SVC run {run}")
x_train, y_train = X[test_index], Y[test_index].ravel()
clf = SVC(C=0.001,kernel='poly')
clf.fit(x_train, y_train)
y_pred = clf.predict(x_train)
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = clf.predict(x_test)
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"SVC Acc Train: {res[0]:.5f}")
P.log(f"SVC F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"SVC Acc Test: {res[2]:.5f}")
P.log(f"SVC F1 Test: {res[3]:.5f}")
def sklearn_baseline(P,V=None):
P.log(P)
F = pp.perform_preprocessing(P, ds.get_data(P,V), P.copy().set_keys( sample_no = None, undersampling = False, oversampling = False, ))
x_train, y_train = F[0]
x_test, y_test = F[2]
y_train, y_test = y_train.ravel(), y_test.ravel()
# P.log('cross_val: '+str(P.get('cross_val')))
# P.log(' FX_num: '+str(P.get('FX_num')))
# ''' Multi-layer Perceptron '''
# res = np.empty(shape=(P.get('runs'),5))
# for run in range(P.get('runs')):
# clf = MLP(hidden_layer_sizes=(100,100),max_iter=500)
# clf.fit(x_train, y_train)
# y_pred = clf.predict(x_train)
# res[run,0] = accuracy_score(y_train,y_pred)
# res[run,1] = f1_score(y_train,y_pred,average='macro')
# y_pred = clf.predict(x_test)
# res[run,2] = accuracy_score(y_test,y_pred)
# res[run,3] = f1_score(y_test,y_pred,average='macro')
# res[run,4] = clf.n_iter_
# res = np.mean(res,axis=0)
# P.log(f"MLP Acc Train: {res[0]:.5f}")
# P.log(f"MLP F1 Train: {res[1]:.5f}")
# P.log(f"MLP Acc Test: {res[2]:.5f}")
# P.log(f"MLP F1 Test: {res[3]:.5f}")
# P.log(F"MLP Iterations = {res[4]}")
''' AdaBoost '''
res = np.empty(shape=(P.get('runs'),4))
for run in range(P.get('runs')):
base = DecisionTreeClassifier(max_depth=10)
clf = AdaBoostClassifier(base_estimator=base, n_estimators=100, learning_rate=0.5, random_state=42)
clf.fit(x_train, y_train)
y_pred = np.round(clf.predict(x_train))
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = np.round(clf.predict(x_test))
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"Ada Acc Train: {res[0]:.5f}")
P.log(f"Ada F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"Ada Acc Test: {res[2]:.5f}")
P.log(f"Ada F1 Test: {res[3]:.5f}")
''' Random Forest Classifier '''
res = np.empty(shape=(P.get('runs'),4))
for run in range(P.get('runs')):
clf = RandomForestClassifier()
clf.fit(x_train, y_train)
y_pred = clf.predict(x_train)
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = clf.predict(x_test)
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"RFC Acc Train: {res[0]:.5f}")
P.log(f"RFC F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"RFC Acc Test: {res[2]:.5f}")
P.log(f"RFC F1 Test: {res[3]:.5f}")
''' Gaussian Naive Bayes '''
res = np.empty(shape=(P.get('runs'),4))
for run in range(P.get('runs')):
clf = GaussianNB()
clf.fit(x_train, y_train)
y_pred = clf.predict(x_train)
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = clf.predict(x_test)
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"GNB Acc Train: {res[0]:.5f}")
P.log(f"GNB F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"GNB Acc Test: {res[2]:.5f}")
P.log(f"GNB F1 Test: {res[3]:.5f}")
''' Support Vector Classification '''
res = np.empty(shape=(P.get('runs'),4))
for run in range(P.get('runs')):
clf = SVC(C=0.001,kernel='poly')
clf.fit(x_train, y_train)
y_pred = clf.predict(x_train)
res[run,0] = accuracy_score(y_train,y_pred)
res[run,1] = f1_score(y_train,y_pred,average='macro')
y_pred = clf.predict(x_test)
res[run,2] = accuracy_score(y_test,y_pred)
res[run,3] = f1_score(y_test,y_pred,average='macro')
res = np.mean(res,axis=0)
P.log("")
P.log(f"SVC Acc Train: {res[0]:.5f}")
P.log(f"SVC F1 Train: {res[1]:.5f}")
#P.log("")
P.log(f"SVC Acc Test: {res[2]:.5f}")
P.log(f"SVC F1 Test: {res[3]:.5f}")
def plt_FX_num(P,max_n=908,P_val=None,indeces=None):
if indeces is not None:max_n = min(max_n,len(indeces))
P.set('FX_indeces',None)
V = ds.get_data(P)
for i,(X,Y) in enumerate(V):
count_string = ', '.join([ f"{int(val)}: {count}" for val,count in zip(*np.unique(Y,return_counts=True))])
P.log(f"V[{i+1}]: {X.shape} - {Y.shape} ({count_string})")
def train_model(X,y,seed):
mlp = MLP(hidden_layer_sizes=(150,150),max_iter=2000, random_state=seed)
return mlp.fit(X, y)
mat = np.empty(shape=(3,max_n))
FX = np.arange(1,max_n+1,1)
for fx in FX:
if indeces is None:P.set('FX_num',fx)
else: P.set('FX_indeces',indeces[:fx])
V0 = ds.select_features(V,P.get('FX_indeces'))
F0 = pp.perform_preprocessing(P, V0, P_val)
x_train, y_train = F0[0]
x_test, y_test = F0[2]
model_list = Parallel(n_jobs=8)(delayed(train_model)(x_train, y_train.ravel(), seed) for seed in range(P.get('runs')))
res = np.empty(shape=(3,P.get('runs')))
for run,mlp in enumerate(model_list):
res[0,run] = accuracy_score(y_test.ravel(),mlp.predict(x_test))
res[1,run] = f1_score(y_test.ravel(),mlp.predict(x_test),average='weighted')
res[2,run] = mlp.n_iter_
mat[:,fx-1] = np.mean(res,axis=1)
P.log(f"Fx_num = {fx}: [Acc = {mat[0,fx-1]:.2f}] [F1 = {mat[1,fx-1]:.2f}] [{mat[2,fx-1]:.2f} iterations]")
plt.figure(figsize=(27,9),dpi=300,clear=True)
fig, ax = plt.subplots()
ax.plot(FX,mat[0],linestyle='solid',label='Accuracy')
ax.plot(FX,mat[1],linestyle='solid',label='F1 Score')
ax.legend()
ax.set_xlabel('FX_num')
ax.set_ylabel('Performance')
ax.set_xlim(1,max_n)
ax.grid()
save_fig(P,'eval_fx_num',fig)
ax.plot(FX,mat[2]/np.max(mat[2]),linestyle='solid',label='Iterations')
ax.legend()
save_fig(P,'eval_fx_num_iterations',fig)
if __name__ == "__main__":
P_fx_num = Params( name='fx_num', dataset='SHL_ext', sample_no=512, undersampling=False, oversampling=False, )
#plt_FX_num(P_fx_num,max_n=fx_num,P_val=P_fx_num.copy().set_keys(sample_no=None, undersampling=False, oversampling=False,))
sklearn_kfolds(P_fx_num,V=None)