-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions_baseline.py
227 lines (160 loc) · 7.41 KB
/
functions_baseline.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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import f1_score
def Objective_knn(trial, X_train, y_train, X_val, y_val):
n_neighbors = trial.suggest_int('n_neighbors', 250, 312, step=1)
weights = trial.suggest_categorical('weights', ['uniform', 'distance'])
metric = trial.suggest_categorical('metric', ['minkowski', 'euclidean', 'manhattan'])
algorithm = trial.suggest_categorical('algorithm', ["auto", "ball_tree", "kd_tree", "brute"])
knn_opt = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, metric=metric, algorithm=algorithm)
knn_opt.fit(X_train, y_train)
y_pred = knn_opt.predict(X_val)
f1_macro = f1_score(y_val, y_pred, average='macro')
return f1_macro
# part of this code is from:
# https://www.kaggle.com/code/mustafagerme/optimization-of-random-forest-model-using-optuna
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
criterion_options = ["gini", "entropy"]
min_samples_leaf_options = [5, 7, 9]
max_features_options = ["sqrt", "log2", None]
bootstrap_options = [True, False]
def Objective_rf(trial, X_train, y_train, X_val, y_val):
random_state = 0
n_estimators = trial.suggest_int("n_estimators", 1, 300, log=True)
max_depth = trial.suggest_int("max_depth", 1, 40)
min_samples_leaf = trial.suggest_categorical("min_samples_leaf", min_samples_leaf_options)
max_features = trial.suggest_categorical("max_features", max_features_options)
bootstrap = trial.suggest_categorical("bootstrap", bootstrap_options)
criterion = trial.suggest_categorical("criterion", criterion_options)
rf_opt = RandomForestClassifier(
max_depth=max_depth,
min_samples_leaf=min_samples_leaf,
max_features=max_features,
bootstrap=bootstrap,
n_estimators=n_estimators,
criterion=criterion,
random_state=random_state)
rf_opt.fit(X_train, y_train)
y_pred = rf_opt.predict(X_val)
f1_macro = f1_score(y_val, y_pred, average='macro')
return f1_macro
# part of this code is is from :
# https://medium.com/@mlxl/knime-xgboost-and-optuna-for-hyper-parameter-optimization-dcf0efdc8ddf
from xgboost import XGBClassifier
from sklearn.metrics import f1_score
from xgboost import XGBClassifier
from sklearn.metrics import f1_score
from xgboost import XGBClassifier
import optuna
def Objective_xgb(trial, X_train, y_train, X_val, y_val):
learning_rate = trial.suggest_float("learning_rate", 0.01, 1.0, log=True)
n_estimators = trial.suggest_int("n_estimators", 100, 1000)
max_depth = trial.suggest_int("max_depth", 3, 30)
min_child_weight = trial.suggest_float("min_child_weight", 0.1, 200.0)
gamma = trial.suggest_float("gamma", 0.0, 1.0)
subsample = trial.suggest_float("subsample", 0.5, 1.0)
colsample_bytree = trial.suggest_float("colsample_bytree", 0.1, 1.0)
reg_alpha = trial.suggest_float("reg_alpha", 0.0, 30.0)
reg_lambda = trial.suggest_float("reg_lambda", 0.0, 30.0)
xgb_opt = XGBClassifier(
learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
min_child_weight=min_child_weight,
gamma=gamma,
subsample=subsample,
colsample_bytree=colsample_bytree,
reg_alpha=reg_alpha,
reg_lambda=reg_lambda,
random_state=0,
objective='multi:softmax',
num_class=5, # 5 modes
tree_method="hist",
predictor="gpu_predictor"
)
xgb_opt.fit(X_train, y_train)
y_pred = xgb_opt.predict(X_val)
f1_macro = f1_score(y_val, y_pred, average='macro')
return f1_macro
# part of this code is from:
# https://www.kaggle.com/code/neilgibbons/tuning-tabnet-with-optuna
from pytorch_tabnet.tab_model import TabNetClassifier
import torch
from sklearn.metrics import f1_score
def Objective_tabnet(trial, X_train, y_train, X_val, y_val):
mask_type = trial.suggest_categorical("mask_type", ["entmax", "sparsemax"])
n_steps = trial.suggest_int("n_steps", 3, 15, step=1)
n_d = trial.suggest_int("n_d", 2, 12, step=2)
gamma = trial.suggest_float("gamma", 1, 1.6, step=0.2)
n_shared = trial.suggest_int("n_shared", 1, 4)
lambda_sparse = trial.suggest_float("lambda_sparse", 1e-6, 1e-3, log=True)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
tabnet_opt = TabNetClassifier(
n_steps=n_steps,
n_d=n_d,
gamma=gamma,
n_shared=n_shared,
lambda_sparse=lambda_sparse,
verbose=0,
device_name=device,
mask_type=mask_type,
optimizer_fn=torch.optim.Adam,
optimizer_params=dict(lr=1e-2),
scheduler_params=dict(
mode="min",
patience=trial.suggest_int("patienceScheduler", low=3, high=10),
min_lr=2e-2,
factor=0.5), scheduler_fn=torch.optim.lr_scheduler.ReduceLROnPlateau)
tabnet_opt.fit(X_train, y_train, batch_size=10024, virtual_batch_size=9000)
y_pred = tabnet_opt.predict(X_val)
f1_macro = f1_score(y_val, y_pred, average='macro')
return f1_macro
# https://github.com/JoseAngelMartinB/prediction-behavioural-analysis-ml-travel-mode-choice/blob/master/expermients_functions.py
import numpy as np
import pandas as pd
from sklearn.metrics import precision_score, recall_score, f1_score, balanced_accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score, balanced_accuracy_score, log_loss
import pandas as pd
# function for different metrics
def all_metrics(y_true, y_pred, y_prob, column_name):
pres_mac = float(precision_score(y_true, y_pred, average='macro'))
rec_mac = float(recall_score(y_true, y_pred, average='macro'))
f1_macro = float(f1_score(y_true, y_pred, average='macro'))
balanced_acc = float(balanced_accuracy_score(y_true, y_pred))
logloss = float(log_loss(y_true, y_prob))
# Put all metrics in a dictionary
evaluation_metrics = {
'Precision (Macro)': str(round(pres_mac * 100, 2)) + "%",
'Recall (Macro)': str(round(rec_mac * 100, 2)) + "%",
'F1-score (Macro)': str(round(f1_macro * 100, 2)) + "%",
'Balanced Accuracy': str(round(balanced_acc * 100, 2)) + "%",
'Log Loss': logloss
}
# Convert dictionary into DataFrame
metrics_df = pd.DataFrame.from_dict(evaluation_metrics, orient='index', columns=[column_name])
return metrics_df
import numpy as np
def predict(classifier, params, X_train, y_train, X_test):
# Predict class labels and probabilities for a given classifier.
# Instantiate classifier
clf = classifier(**params)
# Fit classifier
clf.fit(X_train, y_train)
# Label Prediction
y_pred = clf.predict(X_test)
# Probability prediction
y_pred_proba = clf.predict_proba(X_test)
# Ensure minimum probability
y_pred_proba = np.where(y_pred_proba > 0.00001, y_pred_proba, 0.00001)
return y_pred, y_pred_proba
import pandas as pd
import seaborn as sns
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
def conf_matrix(y_true, y_pred, title, ax):
mapping = {0: 'Class 0', 1: 'Class 1', 2: 'Class 2', 3: 'Class 3', 4: 'Class 4', 5: 'Class 5'}
cm = pd.DataFrame(confusion_matrix(y_true, y_pred), index=mapping.keys(), columns=mapping.keys())
sns.heatmap(cm, annot=True, fmt='g', cmap="Blues", ax=ax)
ax.set_title(title)
ax.set_ylabel('Actual Values')
ax.set_xlabel('Predicted Values')