Created
April 27, 2020 10:57
-
-
Save toshihikoyanase/f962969e2df70276ce76aa5a40ef5e2d to your computer and use it in GitHub Desktop.
Use 'lightgbm_tuner:lgbm_params' in https://gist.github.com/smly/584e4b9eabacecc7c102d47de4066a2f
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 json | |
import mlflow | |
import numpy as np | |
import sklearn.datasets | |
from sklearn.metrics import accuracy_score | |
from sklearn.model_selection import train_test_split | |
import optuna | |
import optuna.integration.lightgbm as lgb | |
from optuna.integration.lightgbm_tuner.optimize import DEFAULT_LIGHTGBM_PARAMETERS | |
def mlflow_callback(study, trial): | |
trial_value = trial.value if trial.value is not None else float("nan") | |
trial_runtime = float("nan") | |
if trial.datetime_start and trial.datetime_complete: | |
trial_runtime = (trial.datetime_complete - trial.datetime_start).total_seconds() | |
with mlflow.start_run(run_name=study.study_name): | |
lgbm_params = json.loads(trial.user_attrs["lightgbm_tuner:lgbm_params"]) | |
tuned_params = {k:lgbm_params[k] for k in DEFAULT_LIGHTGBM_PARAMETERS.keys()} | |
mlflow.log_params(tuned_params) | |
mlflow.log_params({"step_name": trial.user_attrs["lightgbm_tuner:step_name"]}) | |
mlflow.log_metrics({ | |
"trial_number": trial.number, | |
"elapsed_time": trial_runtime, | |
"mean_squared_error": trial_value, | |
}) | |
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", | |
} | |
study = optuna.create_study(study_name="lightgbm_tuner_breast_cancer", direction="minimize") | |
model = lgb.train( | |
params, dtrain, valid_sets=[dtrain, dval], verbose_eval=100, early_stopping_rounds=100, optuna_callbacks=[mlflow_callback] | |
) | |
prediction = np.rint(model.predict(val_x, num_iteration=model.best_iteration)) | |
accuracy = accuracy_score(val_y, prediction) | |
best_params = model.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