-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIG_SUB_Classifier.py
412 lines (328 loc) · 17.1 KB
/
SIG_SUB_Classifier.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
import numpy as np
import pandas as pd
import violation_common
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn.metrics import f1_score, precision_score, recall_score
from sklearn.metrics import balanced_accuracy_score, accuracy_score, cohen_kappa_score, confusion_matrix
from sklearn.linear_model import LogisticRegression
import lightgbm as lgb
from skranger.ensemble import RangerForestClassifier
from torch import nn, optim
import torch
from torch.utils.data import Dataset, DataLoader
import pickle
import dill
from tqdm import tqdm
from pprint import pprint
import optuna
categorical_cols = ['PRIMARY_OR_MILL', 'COAL_METAL_IND', 'MINE_TYPE', 'VIOLATOR_TYPE_CD']
numerical_cols = ['VIOLATOR_INSPECTION_DAY_CNT', 'VIOLATOR_VIOLATION_CNT', 'YEAR_OCCUR']
target = 'SIG_SUB'
feature_ranking = [
'YEAR_OCCUR',
'VIOLATOR_VIOLATION_CNT',
'VIOLATOR_INSPECTION_DAY_CNT',
'MINE_TYPE',
'PRIMARY_OR_MILL',
'COAL_METAL_IND',
'VIOLATOR_TYPE_CD'
]
def lgb_objective(trial, data_train, data_validate, metrics, objective_metric, just_predict=False, return_model=False):
param = {
"objective": "binary",
"metric": "binary_logloss",
"boosting_type": "gbdt",
"verbosity": -2,
"lambda_l1": trial.suggest_float("lambda_l1", 1e-8, 10.0, log=True),
"lambda_l2": trial.suggest_float("lambda_l2", 1e-8, 10.0, log=True),
"num_leaves": trial.suggest_int("num_leaves", 2, 256),
"feature_fraction": trial.suggest_float("feature_fraction", 0.4, 1.0),
"bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 1.0),
"bagging_freq": trial.suggest_int("bagging_freq", 1, 7),
"min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
}
num_features = trial.suggest_int('num_features', 1, len(categorical_cols) + len(numerical_cols))
cols_to_keep = feature_ranking[:num_features]
categorical_cols_to_keep = list(set(cols_to_keep) & set(categorical_cols))
categorical_cols_to_keep.append(target)
numerical_cols_to_keep = list(set(cols_to_keep) & set(numerical_cols))
X_train, y_train, preprocessor = violation_common.df_to_model_ready(data_train, categorical_cols_to_keep, numerical_cols_to_keep, target)
X_validate, y_validate, _ = violation_common.df_to_model_ready(data_validate, categorical_cols_to_keep, numerical_cols_to_keep, target, preprocessor=preprocessor)
model = lgb.LGBMClassifier(**param)
model.fit(X_train, y_train)
preds = model.predict_proba(X_validate)[:, 1]
pred_labels = model.predict(X_validate)
if just_predict:
return pred_labels
if return_model:
return model
# calculate metrics
for metric_name, metric_func, requires_proba in metrics:
metric_value = None
if requires_proba:
metric_value = metric_func(y_validate, preds)
else:
metric_value = metric_func(y_validate, pred_labels)
trial.set_user_attr(metric_name, metric_value)
return trial.user_attrs[objective_metric]
def rf_objective(trial, data_train, data_validate, metrics, objective_metric, just_predict=False, return_model=False):
num_features = trial.suggest_int('num_features', 1, len(categorical_cols) + len(numerical_cols))
param = {
"n_estimators": trial.suggest_int("n_estimators", 64, 128),
"max_depth": trial.suggest_int("max_depth", 2, 10),
"min_node_size": trial.suggest_int("min_node_size", 1, 5),
"mtry": trial.suggest_int("mtry", 0, num_features), # num of features
"split_rule": trial.suggest_categorical("split_rule", ["gini", "extratrees"]),
"sample_fraction": trial.suggest_float("sample_fraction", 0.5, 1.0),
}
cols_to_keep = feature_ranking[:num_features]
categorical_cols_to_keep = list(set(cols_to_keep) & set(categorical_cols))
categorical_cols_to_keep.append(target)
numerical_cols_to_keep = list(set(cols_to_keep) & set(numerical_cols))
X_train, y_train, preprocessor = violation_common.df_to_model_ready(data_train, categorical_cols_to_keep, numerical_cols_to_keep, target)
X_validate, y_validate, _ = violation_common.df_to_model_ready(data_validate, categorical_cols_to_keep, numerical_cols_to_keep, target, preprocessor=preprocessor)
model = RangerForestClassifier(**param)
model.fit(X_train, y_train)
preds = model.predict_proba(X_validate)[:, 1]
pred_labels = model.predict(X_validate)
if just_predict:
return pred_labels
if return_model:
return model
# calculate metrics
for metric_name, metric_func, requires_proba in metrics:
metric_value = None
if requires_proba:
metric_value = metric_func(y_validate, preds)
else:
metric_value = metric_func(y_validate, pred_labels)
trial.set_user_attr(metric_name, metric_value)
return trial.user_attrs[objective_metric]
def logistic_objective(trial, data_train, data_validate, metrics, objective_metric, just_predict=False, return_model=False):
num_features = trial.suggest_int('num_features', 1, len(categorical_cols) + len(numerical_cols))
param = {
"C": trial.suggest_float("C", 1e-10, 1e10, log=True),
"penalty": trial.suggest_categorical("penalty", ["l1", "l2"]),
"solver": trial.suggest_categorical("solver", ["liblinear", "saga"]),
"max_iter": trial.suggest_int("max_iter", 100, 1000),
}
cols_to_keep = feature_ranking[:num_features]
categorical_cols_to_keep = list(set(cols_to_keep) & set(categorical_cols))
categorical_cols_to_keep.append(target)
numerical_cols_to_keep = list(set(cols_to_keep) & set(numerical_cols))
X_train, y_train, preprocessor = violation_common.df_to_model_ready(data_train, categorical_cols_to_keep, numerical_cols_to_keep, target)
X_validate, y_validate, _ = violation_common.df_to_model_ready(data_validate, categorical_cols_to_keep, numerical_cols_to_keep, target, preprocessor=preprocessor)
model = LogisticRegression(**param)
model.fit(X_train, y_train)
preds = model.predict_proba(X_validate)[:, 1]
pred_labels = model.predict(X_validate)
if just_predict:
return pred_labels
if return_model:
return model
# calculate metrics
for metric_name, metric_func, requires_proba in metrics:
metric_value = None
if requires_proba:
metric_value = metric_func(y_validate, preds)
else:
metric_value = metric_func(y_validate, pred_labels)
trial.set_user_attr(metric_name, metric_value)
return trial.user_attrs[objective_metric]
class ClassifierDataset(Dataset):
def __init__(self, features, labels):
self.features = features
self.labels = labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
return torch.tensor(self.features[idx], dtype=torch.float), torch.unsqueeze(torch.tensor(self.labels[idx], dtype=torch.float), dim=0)
def nn_objective(trial, data_train, data_validate, metrics, objective_metric, just_predict=False, return_model=False):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Define neural network architecture
num_features = trial.suggest_int('num_features', 1, len(categorical_cols) + len(numerical_cols))
num_features = trial.suggest_int('num_features', 1, len(categorical_cols) + len(numerical_cols))
cols_to_keep = feature_ranking[:num_features]
categorical_cols_to_keep = list(set(cols_to_keep) & set(categorical_cols))
categorical_cols_to_keep.append(target)
numerical_cols_to_keep = list(set(cols_to_keep) & set(numerical_cols))
X_train, y_train, preprocessor = violation_common.df_to_model_ready(data_train, categorical_cols_to_keep, numerical_cols_to_keep, target)
X_validate, y_validate, _ = violation_common.df_to_model_ready(data_validate, categorical_cols_to_keep, numerical_cols_to_keep, target, preprocessor=preprocessor)
input_size = X_train.shape[1]
dropout_rate = trial.suggest_float('dropout_rate', 0.1, 0.5)
n_layers = trial.suggest_int('n_layers', 1, 3)
layers = []
for i in range(n_layers):
if i == 0:
layers.append(nn.Linear(input_size, trial.suggest_int(f'n_units_l{i}', 10, 500)))
else:
layers.append(nn.Linear(trial.suggest_int(f'n_units_l{i-1}', 10, 500),
trial.suggest_int(f'n_units_l{i}', 10, 500)))
layers.append(nn.ReLU())
layers.append(nn.Dropout(dropout_rate))
layers.append(nn.Linear(trial.suggest_int(f'n_units_l{n_layers-1}', 10, 500), 1))
layers.append(nn.Sigmoid())
model = nn.Sequential(*layers).to(device)
lr = trial.suggest_float("lr", 1e-5, 1e-1, log=True)
optimizer = optim.Adam(model.parameters(), lr=lr)
criterion = nn.BCELoss()
model_train_x, early_stopping_x, model_train_y, early_stopping_y = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
# Create data loaders
model_train_dataset = ClassifierDataset(model_train_x, model_train_y)
val_dataset = ClassifierDataset(X_validate, y_validate)
early_stopping_dataset = ClassifierDataset(early_stopping_x, early_stopping_y)
train_loader = DataLoader(model_train_dataset, batch_size=1024, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)
early_stopping_loader = DataLoader(early_stopping_dataset, batch_size=32, shuffle=False)
early_stopping_best = 0
early_stopping_strikes = 0
# Training loop
for epoch in range(100):
model.train()
for batch in tqdm(train_loader):
features, labels = batch
features, labels = features.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(features)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# early stopping
model.eval()
with torch.no_grad():
preds = []
targets = []
for batch in early_stopping_loader:
features, labels = batch
features, labels = features.to(device), labels.to(device)
outputs = model(features)
preds.extend(outputs.cpu().numpy())
targets.extend(labels.cpu().numpy())
early_stop_auc = roc_auc_score(targets, preds)
print(f'Early stopping auc: {early_stop_auc}')
if early_stop_auc > early_stopping_best + 0.01:
early_stopping_best = early_stop_auc
early_stopping_strikes = 0
else:
if early_stop_auc > early_stopping_best:
early_stopping_best = early_stop_auc
early_stopping_strikes += 1
if early_stopping_strikes == 2:
break
if return_model:
return model
# Validation
model.eval()
preds = []
targets = []
with torch.no_grad():
for batch in val_loader:
features, labels = batch
features, labels = features.to(device), labels.to(device)
outputs = model(features)
preds.extend(outputs.cpu().numpy())
targets.extend(labels.cpu().numpy())
pred_labels = np.array(preds) > 0.5
if just_predict:
return pred_labels
# calculate metrics
for metric_name, metric_func, requires_proba in metrics:
metric_value = None
if requires_proba:
metric_value = metric_func(y_validate, preds)
else:
metric_value = metric_func(y_validate, pred_labels)
trial.set_user_attr(metric_name, metric_value)
return trial.user_attrs[objective_metric]
def hp_tune(train_hp, validate_hp, train_hp_smote, only_save_model=False):
dataset_types = ['non_smote', 'smote']
# model types is a list of tuples of model names and objective functions and number of trials
model_types = [('lightgbm', lgb_objective, 10),
('random_forest', rf_objective, 10),
('logistic_regression', logistic_objective, 10),
('neural_network', nn_objective, 5)]
# list of tuples of metric names and functions along with whether they require a probability prediction
# [('metric_name', metric_function, requires_probability_prediction), ...]
metrics = [('roc_auc_score', roc_auc_score, True),
('accuracy_score', accuracy_score, False),
('balanced_accuracy_score', balanced_accuracy_score, False),
('f1_score', f1_score, False),
('precision_score', precision_score, False),
('recall_score', recall_score, False),
('cohen_kappa_score', cohen_kappa_score, False),
('confusion_matrix', confusion_matrix, False)]
# results[dataset_type][model_name] = study
hp_validation_results = {dataset_type: {model_name[0]: None for model_name in model_types} for dataset_type in dataset_types}
for dataset_type in dataset_types:
for model_name, objective, n_trials in model_types:
print(f'SMOTE: {dataset_type}, Model: {model_name}')
study = optuna.create_study(direction="maximize")
trial_train_data = train_hp_smote if dataset_type == 'smote' else train_hp
study.optimize(lambda trial: objective(trial,
data_train=trial_train_data,
data_validate=validate_hp,
metrics=metrics,
objective_metric='roc_auc_score'),
n_trials=n_trials)
print("Best trial:")
trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
hp_validation_results[dataset_type][model_name] = study
for dataset_type in dataset_types:
for model_name, objective, _ in model_types:
print(f'SMOTE: {dataset_type}, Model: {model_name}')
print("Best hyperparameters:")
pprint(hp_validation_results[dataset_type][model_name].best_params)
print("Metrics:")
pprint(hp_validation_results[dataset_type][model_name].best_trial.user_attrs)
print()
if not only_save_model:
# Save results with pickle
with open('data/hp_validation_results_classifiers.pkl', 'wb') as f:
pickle.dump(hp_validation_results, f)
with open('data/hp_validation_results_classifiers.pkl', 'rb') as f:
hp_validation_results = pickle.load(f)
print(hp_validation_results)
# save best models
for dataset_type in dataset_types:
for model_name, objective, _ in model_types:
print(f'SMOTE: {dataset_type}, Model: {model_name}')
best_trial = hp_validation_results[dataset_type][model_name].best_trial
best_params = best_trial.params
best_model = objective(best_trial, train_hp, validate_hp, metrics, 'roc_auc_score', return_model=True)
# save with pickle and dill just in case
with open(f'data/best_models/{model_name}_{dataset_type}.pkl', 'wb') as f, open(f'data/best_models/{model_name}_{dataset_type}.dill', 'wb') as f2:
pickle.dump(best_model, f)
dill.dump(best_model, f2)
# now save with
if __name__ == '__main__':
# Load data
train_full = pd.read_csv('data/after_2010_train_full.csv', index_col=0)
test = pd.read_csv('data/after_2010_test.csv', index_col=0)
train_hp = pd.read_csv('data/after_2010_train_hp.csv', index_col=0)
validate_hp = pd.read_csv('data/after_2010_validate_hp.csv', index_col=0)
train_smote_full = pd.read_csv('data/after_2010_train_smote_full.csv', index_col=0)
train_hp_smote = pd.read_csv('data/after_2010_train_smote_hp.csv', index_col=0)
dataset_types = ['non_smote', 'smote']
# model types is a list of tuples of model names and objective functions and number of trials
model_types = [('lightgbm', lgb_objective, 10),
('random_forest', rf_objective, 10),
('logistic_regression', logistic_objective, 10),
('neural_network', nn_objective, 5)]
# with open('data/hp_validation_results_classifiers.pkl', 'rb') as f:
# hp_validation_results = pickle.load(f)
# for dataset_type in dataset_types:
# for model_name, objective, _ in model_types:
# print(f'SMOTE: {dataset_type}, Model: {model_name}')
# print("Best hyperparameters:")
# pprint(hp_validation_results[dataset_type][model_name].best_params)
# print("Metrics:")
# pprint(hp_validation_results[dataset_type][model_name].best_trial.user_attrs)
# print('Confusion Matrix:')
# pprint(hp_validation_results[dataset_type][model_name].best_trial.user_attrs['confusion_matrix'])
# print()
hp_tune(train_hp, validate_hp, train_hp_smote, only_save_model=True)