-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-multirun.py
39 lines (32 loc) · 1.5 KB
/
main-multirun.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
import optuna
import pandas as pd
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score
from models import random_forest, log_reg
from preprocessing_multirun import simple_preprocess, complex_preprocess
from optuna.samplers import TPESampler
from omegaconf import DictConfig
import hydra
from hydra.utils import to_absolute_path
@hydra.main(config_name='config', config_path='conf')
def train_model(cfg: DictConfig):
train_df_small = pd.read_csv(to_absolute_path(cfg.preprocess.data_train))
test_df_small = pd.read_csv(to_absolute_path(cfg.preprocess.data_test))
X_train, y_train = train_df_small.drop(columns = 'Survived'), train_df_small.Survived
X_test, y_test = test_df_small.drop(columns = 'Survived'), test_df_small.Survived
def optimize_model(trial):
preprocess = hydra.utils.call(cfg.preprocess.type, trial = trial)
model_pipe = Pipeline(steps = [
('prep', preprocess),
('model', hydra.utils.call(cfg.models.type, trial = trial))
])
model_pipe.fit(X_train,y_train)
y_pred = model_pipe.predict(X_test)
return accuracy_score(y_test, y_pred)
sampler = TPESampler(seed=123)
study = optuna.create_study(sampler = sampler, direction="maximize")
study.optimize(optimize_model, n_trials=cfg.n_trials)
print(f'El mejor accuracy conseguido fue: {study.best_value}')
print(f'usando los siguientes parámetros: \n \t \t{study.best_params}')
if __name__ == '__main__':
train_model()