Skip to content

Instantly share code, notes, and snippets.

@toshihikoyanase
Created April 8, 2020 08:31
Show Gist options
  • Save toshihikoyanase/83f47966bef0f5d9d147d013742a3e85 to your computer and use it in GitHub Desktop.
Save toshihikoyanase/83f47966bef0f5d9d147d013742a3e85 to your computer and use it in GitHub Desktop.
An example of parallel execution of LightGBMTuner.
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")
"""
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))
@shen2014
Copy link

shen2014 commented Mar 7, 2022

I am running this script on the same dataset. But there seems to be an error in line 45 :

prediction = np.rint(model.predict(test_x, num_iteration=model.best_iteration))
AttributeError: 'NoneType' object has no attribute 'predict'

thanks in advance!

@ninfueng
Copy link

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