Created
April 8, 2020 08:31
-
-
Save toshihikoyanase/83f47966bef0f5d9d147d013742a3e85 to your computer and use it in GitHub Desktop.
An example of parallel execution of LightGBMTuner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import optuna | |
study = optuna.create_study( | |
storage="sqlite:///lgbtuner.db", study_name="parallel", load_if_exists=True | |
) | |
study.trials_dataframe().to_csv("parallel-result.csv") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Optuna example that optimizes a classifier configuration for cancer dataset using LightGBM tuner. | |
In this example, we optimize the validation log loss of cancer detection. | |
You can execute this code directly. | |
$ python lightgbm_tuner_parallel.py | |
""" | |
import lightgbm as lgb | |
import numpy as np | |
import sklearn.datasets | |
from sklearn.metrics import accuracy_score | |
from sklearn.model_selection import train_test_split | |
import optuna | |
from optuna.integration.lightgbm import LightGBMTuner | |
data, target = sklearn.datasets.load_breast_cancer(return_X_y=True) | |
train_x, val_x, train_y, val_y = train_test_split(data, target, test_size=0.25, random_state=1) | |
dtrain = lgb.Dataset(train_x, label=train_y) | |
dval = lgb.Dataset(val_x, label=val_y) | |
params = { | |
"objective": "binary", | |
"metric": "binary_logloss", | |
"verbosity": -1, | |
"boosting_type": "gbdt", | |
} | |
study = optuna.create_study( | |
storage="sqlite:///lgbtuner.db", study_name="parallel", load_if_exists=True | |
) | |
tuner = LightGBMTuner( | |
params, | |
dtrain, | |
valid_sets=[dtrain, dval], | |
verbose_eval=100, | |
early_stopping_rounds=100, | |
study=study, | |
) | |
model = tuner.run() | |
prediction = np.rint(model.predict(val_x, num_iteration=model.best_iteration)) | |
accuracy = accuracy_score(val_y, prediction) | |
best_params = model.params | |
print("Number of finished trials: {}".format(len(study.trials))) | |
print("Best params:", best_params) | |
print(" Accuracy = {}".format(accuracy)) | |
print(" Params: ") | |
for key, value in best_params.items(): | |
print(" {}: {}".format(key, value)) |
I also got the same problem as @shen2014 . tuner.run()
returns None
, hence predict
attribute cannot be accessed. Using model = tuner.get_best_booster()
should fix this problem.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am running this script on the same dataset. But there seems to be an error in line 45 :
thanks in advance!