Tuning with optuna (Experimental)

To run this code, you need to have optuna installed.

Installation

# To install the required package, use the following command:
# !pip install modeva

Authentication

# To get authentication, use the following command: (To get full access please replace the token to your own token)
# from modeva.utils.authenticate import authenticate
# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')

Import required modules

from modeva import DataSet
from modeva import TestSuite
from modeva.models import MoLGBMClassifier
from modeva.models import ModelTuneOptuna
from scipy.stats import uniform, randint

Load Dataset

ds = DataSet()
ds.load(name="SimuCredit")
ds.set_random_split()

Run HPO

param_distributions = {"max_depth": [1, 2, 3],
                       "learning_rate": uniform(0.01, 0.3),
                       "n_estimators": randint(1, 100),
                      }

model = MoLGBMClassifier(verbose=-1)
hpo = ModelTuneOptuna(dataset=ds, model=model)
result = hpo.run(param_distributions=param_distributions,
                 sampler="tpe", # "grid", "random", "tpe", "gs", "cma-es", "qmc"
                 metric=("AUC", "ACC", "LogLoss"),
                 cv=5)
result.table
max_depth learning_rate n_estimators AUC ACC LogLoss AUC_rank ACC_rank LogLoss_rank mean_fit_time
4 3 0.1184 64 0.8384 0.7568 0.4908 1 1 1 0.0601
5 3 0.0702 53 0.8341 0.7540 0.5004 2 2 2 0.0548
8 2 0.1490 34 0.8301 0.7529 0.5091 3 3 3 0.0345
9 2 0.0478 74 0.8260 0.7474 0.5188 4 5 5 0.0590
6 1 0.1442 99 0.8243 0.7500 0.5151 5 4 4 0.0550
7 1 0.1502 32 0.8129 0.7417 0.5411 6 6 6 0.0285
3 2 0.0517 21 0.8039 0.7346 0.5669 7 7 7 0.0277
1 1 0.0278 79 0.8004 0.7174 0.5744 8 8 8 0.0471
0 1 0.1570 10 0.7949 0.7172 0.5855 9 9 9 0.0197
2 2 0.0422 7 0.7885 0.7157 0.6311 10 10 10 0.0190


result.plot("parallel", figsize=(8, 6))


result.plot(("max_depth", "AUC"))


result.plot(("learning_rate", "AUC"))


result.plot(("n_estimators", "AUC"))


Retrain model with best hyperparameter

model_tuned = MoLGBMClassifier(**result.value["params"][0],
                               name="LGBM-Tuned",
                               verbose=-1)
model_tuned.fit(ds.train_x, ds.train_y)
model_tuned
MoLGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,
                 importance_type='split', learning_rate=0.15704454364210055,
                 max_depth=1, min_child_samples=20, min_child_weight=0.001,
                 min_split_gain=0.0, n_estimators=10, n_jobs=None,
                 num_leaves=31, objective=None, random_state=None,
                 reg_alpha=0.0, reg_lambda=0.0, subsample=1.0,
                 subsample_for_bin=200000, subsample_freq=0, verbose=-1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Diagnose the tuned model

ts = TestSuite(ds, model_tuned)
result = ts.diagnose_accuracy_table()
result.table
AUC ACC F1 LogLoss Precision Recall Brier
train 0.7964 0.7174 0.7494 0.5848 0.7344 0.7650 0.1980
test 0.7973 0.7205 0.7487 0.5839 0.7380 0.7596 0.1975
GAP 0.0009 0.0031 -0.0008 -0.0010 0.0036 -0.0054 -0.0005


Total running time of the script: (0 minutes 2.907 seconds)

Gallery generated by Sphinx-Gallery