python 21 lines · 1 tab

Bayesian optimization with Optuna for efficient model tuning

1 tab
import optuna
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import cross_val_score

def objective(trial):
    model = HistGradientBoostingClassifier(
        learning_rate=trial.suggest_float('learning_rate', 0.01, 0.2, log=True),
        max_depth=trial.suggest_int('max_depth', 3, 12),
        max_leaf_nodes=trial.suggest_int('max_leaf_nodes', 15, 63),
        min_samples_leaf=trial.suggest_int('min_samples_leaf', 10, 100),
        l2_regularization=trial.suggest_float('l2_regularization', 1e-6, 1.0, log=True),
        random_state=42,
    )
    scores = cross_val_score(model, X_train, y_train, cv=5, scoring='roc_auc', n_jobs=-1)
    return scores.mean()

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=50)

print(study.best_trial.params)
print(study.best_value)
1 file · python Explain with highlit

When the search space is wide, Optuna gives me better signal per compute dollar than brute-force sweeps. It is easy to define conditional search spaces, prune bad trials early, and track the best trial artifacts. I especially like it for gradient boosting and neural network tuning.

Share this code

Here's the card — post it anywhere.

Bayesian optimization with Optuna for efficient model tuning — share card
Link copied