Bayesian optimisation fits a cheap model of how settings affect the score and uses it to pick the most promising next trial; Optuna’s default sampler, TPE, works this way. Successive halving starts many candidates on a small budget, such as a sample of rows, and gives more to the best each round; scikit-learn’s HalvingRandomSearchCV implements it, still as experimental. Random search remains the baseline these methods must beat.
import optuna
from sklearn.model_selection import cross_val_score
from xgboost import XGBClassifier
def objective(trial):
model = XGBClassifier(max_depth=trial.suggest_int("max_depth", 3, 10),
learning_rate=trial.suggest_float("lr", 0.01, 0.3, log=True))
return cross_val_score(model, X_train, y_train, cv=5, scoring="roc_auc").mean()
study = optuna.create_study(direction="maximize") # TPE sampler by default
study.optimize(objective, n_trials=50)
Tune on cross-validation folds of the training data, and touch the test set once, at the end.
Snoek, Larochelle and Adams (2012), Practical Bayesian optimization of machine learning algorithms, NIPSOptuna: TPESamplerOptuna: create_study (the default sampler)scikit-learn: 3.2 Tuning the hyper-parameters of an estimator (successive halving)Bergstra and Bengio (2012), Random search for hyper-parameter optimization, JMLR
Question adapted from data-science-interviews by Alexey Grigorev and contributors, CC BY 4.0; reworded, with our own answer.
Practise it with Prism