-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpkpts_train.py
325 lines (260 loc) · 9.73 KB
/
mpkpts_train.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
"""
This file trains a model on the extracted features using statistical methods.
"""
import argparse
import pickle
from pathlib import Path
import numpy as np
import pandas as pd
from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
f1_score,
precision_score,
recall_score,
roc_auc_score,
)
from sklearn.model_selection import GridSearchCV, StratifiedKFold
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
import wandb
from src import constants
from wandb.sklearn import plot_class_proportions, plot_precision_recall, plot_roc
def build_grid_search(estimator, param_grid):
grid_search = GridSearchCV(
estimator=estimator,
param_grid=param_grid,
cv=StratifiedKFold(),
n_jobs=-1,
verbose=2,
scoring="accuracy",
)
return grid_search
parser = argparse.ArgumentParser()
parser.add_argument(
"--folder",
type=str,
required=True,
help="Folder containing the extracted features.",
)
args = parser.parse_args()
path_data = Path(args.folder)
# Read data
X_train = pd.read_pickle(path_data / "X_train.pkl")
X_test = pd.read_pickle(path_data / "X_test.pkl")
y_train = pd.read_pickle(path_data / "y_train.pkl")
y_test = pd.read_pickle(path_data / "y_test.pkl")
# Read final features
with open(str(path_data / "final_features.pkl"), "rb") as handle:
final_features = pickle.load(handle)
# Subset data to final features
X_train = X_train[final_features]
X_test = X_test[final_features]
# Normalize data
scaler = StandardScaler()
X_train = pd.DataFrame(scaler.fit_transform(X_train), columns=X_train.columns)
X_test = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns)
# Save scaler
with open(str(path_data / "scaler.pkl"), "wb") as handle:
pickle.dump(scaler, handle, protocol=pickle.HIGHEST_PROTOCOL)
training_results = {}
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("Random Forest")
param_grid = {
"n_estimators": [150, 300, 450],
"max_depth": [None, 8, 16, 24],
"min_samples_split": [1, 8, 16, 24],
"min_samples_leaf": [1, 8, 16, 24],
"random_state": [constants.SEED],
}
rf = RandomForestClassifier()
grid_search = build_grid_search(rf, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters:", grid_search.best_params_)
training_results["RF"] = {}
training_results["RF"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["RF"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["RF"]["config"] = grid_search.best_estimator_.get_params()
training_results["RF"]["model"] = grid_search
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("SVM")
param_grid = {
"C": [1],
"gamma": ["scale"],
"kernel": ["rbf"],
"random_state": [constants.SEED],
}
svc = SVC(probability=True)
grid_search = build_grid_search(svc, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters SVM:", grid_search.best_params_)
training_results["SVM"] = {}
training_results["SVM"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["SVM"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["SVM"]["config"] = grid_search.best_estimator_.get_params()
training_results["SVM"]["model"] = grid_search
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("Logistic Regression")
param_grid = {
"C": [0.01, 0.1, 1, 10, 100],
"penalty": ["l1", "l2"],
"solver": ["liblinear"],
"max_iter": [1000],
"random_state": [constants.SEED],
}
lr = LogisticRegression()
grid_search = build_grid_search(lr, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters LR:", grid_search.best_params_)
training_results["LR"] = {}
training_results["LR"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["LR"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["LR"]["config"] = grid_search.best_estimator_.get_params()
training_results["LR"]["model"] = grid_search
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("KNN")
param_grid = {
"n_neighbors": [1, 3, 5, 7, 9],
"weights": ["uniform", "distance"],
"p": [1, 2],
}
knn = KNeighborsClassifier()
grid_search = build_grid_search(knn, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters KNN:", grid_search.best_params_)
training_results["KNN"] = {}
training_results["KNN"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["KNN"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["KNN"]["config"] = grid_search.best_estimator_.get_params()
training_results["KNN"]["model"] = grid_search
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("MLPClassifier")
param_grid = {
"hidden_layer_sizes": [(100,), (100, 100), (100, 100, 100)],
"activation": ["relu"],
"solver": ["adam"],
"alpha": [0.0001],
"batch_size": ["auto"],
"learning_rate": ["constant"],
"learning_rate_init": [0.001],
"max_iter": [200],
"random_state": [constants.SEED],
}
mlp = MLPClassifier()
grid_search = build_grid_search(mlp, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters MLP:", grid_search.best_params_)
training_results["MLP"] = {}
training_results["MLP"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["MLP"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["MLP"]["config"] = grid_search.best_estimator_.get_params()
training_results["MLP"]["model"] = grid_search
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("AdaBoostClassifier")
param_grid = {
"n_estimators": [50, 100, 150],
"learning_rate": [0.01, 0.1, 1],
"random_state": [constants.SEED],
}
ada = AdaBoostClassifier()
grid_search = build_grid_search(ada, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters Ada:", grid_search.best_params_)
training_results["Ada"] = {}
training_results["Ada"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["Ada"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["Ada"]["config"] = grid_search.best_estimator_.get_params()
training_results["Ada"]["model"] = grid_search
# -----------------------------------------------------------------------------
print("----------------------------------------")
print("GaussianNB")
param_grid = {
"var_smoothing": [1e-09],
}
gnb = GaussianNB()
grid_search = build_grid_search(gnb, param_grid)
grid_search.fit(X_train, y_train)
print("Best parameters GNB:", grid_search.best_params_)
training_results["GNB"] = {}
training_results["GNB"]["y_proba_test"] = grid_search.best_estimator_.predict_proba(
X_test
)
training_results["GNB"]["y_proba_train"] = grid_search.best_estimator_.predict_proba(
X_train
)
training_results["GNB"]["config"] = grid_search.best_estimator_.get_params()
training_results["GNB"]["model"] = grid_search
# -----------------------------------------------------------------------------
# Save training_results
with open(str(path_data / "training_results.pkl"), "wb") as handle:
pickle.dump(training_results, handle, protocol=pickle.HIGHEST_PROTOCOL)
# -----------------------------------------------------------------------------
# Evaluate performance on wandb
# Iterate on training_results
for classifier_name, model_details in training_results.items():
# start a new wandb run and add your model hyperparameters
wandb.init(
project=constants.MODEL_NAME,
config=model_details.get("config"),
name=classifier_name + "_ext_neg_teo",
)
# log additional visualisations to wandb
plot_class_proportions(y_train, y_test, constants.LABELS)
# plot_learning_curve(model_details.get('model'), X_train, y_train)
plot_roc(y_test, model_details.get("y_proba_test"), constants.LABELS)
plot_precision_recall(y_test, model_details.get("y_proba_test"), constants.LABELS)
# log metrics to wandb
y_pred_test = np.where(model_details.get("y_proba_test")[:, 1] > 0.5, 1, 0)
y_pred_train = np.where(model_details.get("y_proba_train")[:, 1] > 0.5, 1, 0)
wandb.log(
{
"test_accuracy": accuracy_score(y_test, y_pred_test),
"test_auc": roc_auc_score(y_test, model_details.get("y_proba_test")[:, 1]),
"test_precision": precision_score(y_test, y_pred_test),
"test_recall": recall_score(y_test, y_pred_test),
"test_f1": f1_score(y_test, y_pred_test),
"train_accuracy": accuracy_score(y_train, y_pred_train),
"train_auc": roc_auc_score(
y_train, model_details.get("y_proba_train")[:, 1]
),
"train_precision": precision_score(y_train, y_pred_train),
"train_recall": recall_score(y_train, y_pred_train),
"train_f1": f1_score(y_train, y_pred_train),
}
)
# [optional] finish the wandb run, necessary in notebooks
wandb.finish()