-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_experiments.py
439 lines (381 loc) · 15.7 KB
/
run_experiments.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# SPDX-FileCopyrightText: 2024 Nico Hambauer, Sven Kruschel
#
# SPDX-License-Identifier: MIT
import os
import warnings
import numpy as np
import pandas as pd
import json
import itertools
from numpy.linalg import LinAlgError
from pygam.utils import OptimizationError
from sklearn.metrics import mean_squared_error, log_loss
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import StratifiedKFold, KFold, train_test_split
from datetime import datetime
from logging_reports import JournalLogger
from load_datasets import Dataset
from model import Model
"""
Traditional Models:
- LR (Elasticnet, Lasso, Ridge)
- RF
- XGB
- DT
GAMs:
- PYGAM
- EBM
- NAM
- GAMINET
- EXNN
- IGANN
"""
random_state = 42
verbose = 2
n_folds = 5
hyperparameter_config_file = "./default_hyperparams.json"
tasks = ["classification", "regression"]
traditional_models_to_run = [
"TABNET",
"LR",
"DT",
"RF",
"XGB",
"MLP",
"CATBOOST",
]
gam_models_to_run = [
"PYGAM",
"EBM",
"NAM",
"GAMINET",
"EXNN",
"IGANN",
]
classification_datasets = [
"stroke",
"adult",
"telco",
"college",
"fico",
"bank",
"airline",
"compas",
"water",
"weather",
]
regression_datasets = [
"car",
"crab",
"medical",
"productivity",
"student",
"crimes",
"bike",
"housing",
"diamond",
"wine",
]
for task in tasks:
directory = f"./tabnet_test/tuning/{task}"
# read or create the best hpo config csvs
best_hpo_config_csvs = []
for i in range(1, n_folds + 1):
if not os.path.exists(f"{directory}/hpo_best_config_Fold_{i}.csv"):
best_hpo_config_csvs.append(
pd.DataFrame(
index=classification_datasets + regression_datasets,
columns=traditional_models_to_run + gam_models_to_run,
)
)
else:
best_hpo_config_csvs.append(
pd.read_csv(
f"{directory}/hpo_best_config_Fold_{i}.csv", index_col=0, header=0
)
)
datasets_to_run_on = None
if task == "classification":
datasets_to_run_on = classification_datasets
elif task == "regression":
datasets_to_run_on = regression_datasets
with open(hyperparameter_config_file, "r") as read_file:
hpo_grid = json.load(read_file)
for model_name in traditional_models_to_run + gam_models_to_run:
for dataset_name in datasets_to_run_on:
if model_name == "LR" and task == "regression":
# use the sklearn Elasticnet for regression instead of LR
model_name = "ELASTICNET"
keys, values = zip(*hpo_grid[model_name].items())
# Compute all combinations from the hpo grid
permutations_dicts = [
dict(zip(keys, v)) for v in itertools.product(*values)
]
if model_name == "LR":
condition = lambda args: (
(args["solver"] == "lbfgs" and args["penalty"] == "l1")
or (args["solver"] == "lbfgs" and args["penalty"] == "elasticnet")
or
# ‘lbfgs’ only works with [‘l2’, None]
(args["solver"] == "liblinear" and args["penalty"] == "elasticnet")
or (args["solver"] == "liblinear" and args["penalty"] == "none")
or
# ‘liblinear’ only works with [‘l1’, ‘l2’]
(
isinstance(args["l1_ratio"], float)
and args["penalty"] != "elasticnet"
)
or
# l1_ratio is only used when penalty is elasticnet
(args["l1_ratio"] is None and args["penalty"] == "elasticnet")
# when elasticnet is used l1_ratio must be not None
)
permutations_dicts = [
item for item in permutations_dicts if not condition(item)
]
if (model_name == "RF" or model_name == "DT") and task == "regression":
condition = lambda args: args["class_weight"] == "balanced"
permutations_dicts = [
item for item in permutations_dicts if not condition(item)
]
logger = JournalLogger()
logger.set_global_result_dir(directory)
print("\n", "#" * 3, f"Run experiment on {dataset_name}", "#" * 3)
# load dataset
dataset = Dataset(dataset_name, model_name)
X = dataset.X
y = dataset.y
# We use Inner Split - outer Cross validation
# The purpose is in the outer cv to get an estimation of the test error.
# The inner split val is used to tune the hyperparameters of the model.
# we made the tradeoff of using an inner split instead another cv loop to reduce the computational cost.
outer_cv = None
if task == "classification":
outer_cv = StratifiedKFold(
n_splits=n_folds, shuffle=True, random_state=random_state
)
elif task == "regression":
outer_cv = KFold(
n_splits=n_folds, shuffle=True, random_state=random_state
)
for fold_i, (train_val_idx, test_idx) in enumerate(outer_cv.split(X, y)):
print(
"\n",
"-" * 5,
"Model:",
model_name,
"-- Fold:",
fold_i + 1,
"/",
n_folds,
"-" * 5,
)
X_train_val, y_train_val = X.iloc[train_val_idx], y.iloc[train_val_idx]
X_test, y_test = X.iloc[test_idx], y.iloc[test_idx]
if task == "regression":
y_scaler = StandardScaler()
# scale the target out of sample for regression
y_train_val = pd.Series(
y_scaler.fit_transform(
y_train_val.values.reshape(-1, 1)
).flatten()
)
y_test = pd.Series(
y_scaler.transform(y_test.values.reshape(-1, 1)).flatten()
)
# one hot encoder pipeline drops the original categorical columns if binary. That means two
# categories male and female become one column e.g. female = 0 or 1
cat_step = (
"ohe",
OneHotEncoder(
sparse=False, handle_unknown="ignore", drop="if_binary"
),
)
# Our pre-study showed, that encoding the categories in our pipeline is faster and better for performance.
# if model_name == "CATBOOST":
# cat_step = ("identity", "passthrough")
cat_pipe = Pipeline([cat_step])
num_pipe = Pipeline([("scaler", StandardScaler())])
transformers = [
("cat", cat_pipe, dataset.categorical_cols),
("num", num_pipe, dataset.numerical_cols),
]
ct = ColumnTransformer(transformers=transformers)
# split val the data into train and val
if task == "classification":
X_train, X_val, y_train, y_val = train_test_split(
X_train_val,
y_train_val,
test_size=0.25,
stratify=y_train_val,
random_state=1337,
)
elif task == "regression":
X_train, X_val, y_train, y_val = train_test_split(
X_train_val, y_train_val, test_size=0.25, random_state=1337
)
ct.fit(X_train)
X_train = pd.DataFrame(
ct.transform(X_train), columns=ct.get_feature_names_out()
)
X_val = pd.DataFrame(
ct.transform(X_val), columns=ct.get_feature_names_out()
)
all_transformed_feature_names = ct.get_feature_names_out()
# Now you have the correctly mapped and ordered lists of transformed feature names
transformed_numerical_names = [
name
for name in all_transformed_feature_names
if name.startswith("num__")
]
transformed_categorical_names = [
name
for name in all_transformed_feature_names
if name.startswith("cat__")
]
print("Dataset Categorical Columns:", dataset.categorical_cols)
print("Dataset Numerical Columns:", dataset.numerical_cols)
print("Transformed Categorical Columns:", transformed_categorical_names)
print("Transformed Numerical Columns:", transformed_numerical_names)
if model_name == "MLP":
X_train = X_train.values
X_val = X_val.values
if verbose == 1:
print("")
best_hp_config = None
best_loss = np.inf
training_time_of_best_model = np.inf
timings_hpo = []
# tuning hyperparameters in case of multiple hyperparameter candidates
logger.set_current_dataset_model_dir(dataset_name, model_name)
for id, arg_dict in enumerate(permutations_dicts):
# print the progress with replacing in line all the time
if verbose == 1:
print(
"\r",
"Progress: ",
id + 1,
"/",
len(permutations_dicts),
end="",
)
elif verbose == 2:
print("-" * 20)
print(arg_dict)
# define the model
model = Model(
model_name,
task,
arg_dict,
num_cols=transformed_numerical_names,
cat_cols=transformed_categorical_names,
)
start_training_time = datetime.now()
try:
# fit the model
model.fit(X_train, y_train)
except (LinAlgError, OptimizationError) as e:
print(e)
warnings.warn(
"Training with this hp combination, Error in Gaminet (Optimization Error, warm start) or Pygam (LinAlgError) possible"
)
continue
training_time = (
datetime.now() - start_training_time
).total_seconds()
timings_hpo.append(training_time)
if task == "regression":
# calculate the mse
y_pred = model.predict(X_val)
mse = mean_squared_error(y_val, y_pred)
if mse < best_loss:
best_hp_config = arg_dict
best_loss = mse
training_time_of_best_model = training_time
elif task == "classification":
# calculate the loss
y_pred = model.predict(X_val)
ce_loss = log_loss(y_val, y_pred)
if ce_loss < best_loss:
best_hp_config = arg_dict
best_loss = ce_loss
training_time_of_best_model = training_time
best_hpo_string = (
str(best_hp_config)
.replace("{", "")
.replace("}", "")
.replace(",", "\n")
)
best_hpo_config_csvs[fold_i].loc[
dataset_name, model_name
] = best_hpo_string
# now take the best hpo config and retrain on X_train_val and y_train_val
ct_test = ColumnTransformer(transformers=transformers)
ct_test.fit(X_train_val)
X_train_val = pd.DataFrame(
ct_test.transform(X_train_val),
columns=ct_test.get_feature_names_out(),
)
X_test = pd.DataFrame(
ct_test.transform(X_test),
columns=ct_test.get_feature_names_out(),
)
all_transformed_feature_names = ct_test.get_feature_names_out()
transformed_numerical_names = [
name
for name in all_transformed_feature_names
if name.startswith("num__")
]
transformed_categorical_names = [
name
for name in all_transformed_feature_names
if name.startswith("cat__")
]
# Now you have the correctly mapped and ordered lists of transformed feature names
print("Transformed Categorical Columns:", transformed_categorical_names)
print("Transformed Numerical Columns:", transformed_numerical_names)
if model_name == "MLP":
X_train_val = X_train_val.values
X_test = X_test.values
best_model = Model(
model_name,
task,
best_hp_config,
num_cols=transformed_numerical_names,
cat_cols=transformed_categorical_names,
)
try:
best_model.fit(X_train_val, y_train_val)
except (OptimizationError, LinAlgError) as e:
print(e)
warnings.warn(
"Training with this hp combination, Error in Gaminet (Optimization Error, warm start) or Pygam (LinAlgError) possible"
)
continue
else:
# evaluate the retrained best model on the hold out dataset
y_pred = best_model.predict(X_test)
if task == "classification":
y_pred_proba = best_model.predict_proba(X_test)
if task == "classification":
logger.log_classification_report(
y_true=y_test, y_pred=y_pred, dataset=dataset, k_fold=fold_i
)
logger.log_roc_auc(
y_true=y_test, y_pred_confidence=y_pred_proba, k_fold=fold_i
)
elif task == "regression":
logger.log_regression_report(
y_true=y_test, y_pred=y_pred, k_fold=fold_i
)
logger.log_timing(
training_time_of_best_model, np.mean(timings_hpo), fold_i
)
for i in range(n_folds):
best_hpo_config_csvs[i].to_csv(
f"{directory}/hpo_best_config_Fold_{i + 1}.csv",
index=True,
header=True,
)