Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
xgboost + optuna
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
You can’t perform that action at this time.