Skip to content

Instantly share code, notes, and snippets.

@toshihikoyanase
Created October 27, 2020 07:50
Show Gist options
  • Save toshihikoyanase/bb60cb68d0d09a9c83c700ad73e02f59 to your computer and use it in GitHub Desktop.
Save toshihikoyanase/bb60cb68d0d09a9c83c700ad73e02f59 to your computer and use it in GitHub Desktop.
An example of `StepwiseLightGBMTuner`.
import numpy as np
import sklearn.datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import optuna.integration.lightgbm as lgb
if __name__ == "__main__":
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)
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",
}
model = lgb.StepwiseLightGBMTuner(
params, dtrain, valid_sets=[dtrain, dval], verbose_eval=100, early_stopping_rounds=100
)
model.optimize()
booster = model.get_best_booster()
prediction = np.rint(booster.predict(val_x, num_iteration=booster.best_iteration))
accuracy = accuracy_score(val_y, prediction)
best_params = model.best_params
print("Best params:", best_params)
print(" Accuracy = {}".format(accuracy))
print(" Params: ")
for key, value in best_params.items():
print(" {}: {}".format(key, value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment