Last active
September 11, 2019 11:53
-
-
Save ymtky/d64281fd123d11b9e52ca7c3ae9b526f to your computer and use it in GitHub Desktop.
xgboost + optuna
This file contains 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 | |
from functools import partial | |
import xgboost as xgb | |
from sklearn.metrics import f1_score | |
def objective(dtrain, dtest, trial): | |
max_depth = trial.suggest_int('max_depth',1,10) | |
eta = trial.suggest_uniform('eta',0.0,1) | |
subsample = trial.suggest_uniform('subsample', 0.5, 1) | |
colsample_bytree = trial.suggest_uniform('colsample_bytree', 0.5, 1) | |
min_child_weight = trial.suggest_uniform('min_child_weight', 0.5, 3.0) | |
xgb_params = { | |
'objective': 'binary:logistic', | |
'eval_metric': 'logloss', | |
'max_depth': max_depth, | |
'eta': eta, | |
'colsample_bytree': colsample_bytree, | |
'subsample': subsample, | |
'min_child_weight': min_child_weight | |
} | |
bst = xgb.train(xgb_params, | |
dtrain, | |
num_boost_round=100, | |
) | |
pred_proba = bst.predict(dtest) | |
pred = np.where(pred_proba > 0.5, 1, 0) | |
return 1.0 - f1_score(pred, y_test) | |
dtrain = xgb.DMatrix(X_train, label=y_train) | |
dtest = xgb.DMatrix(X_test, label=y_test) | |
study = optuna.create_study() | |
study.optimize(partial(objective, dtrain, dtest), n_trials=100) | |
study.best_params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment