pythonintermediate

Optuna Hyperparameter Optimization

Automate hyperparameter search with Optuna using Bayesian optimization and pruning.

python
import optuna
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
import warnings
warnings.filterwarnings('ignore')

X, y = load_iris(return_X_y=True)

def objective(trial: optuna.Trial) -> float:
    params = {
        'n_estimators':  trial.suggest_int('n_estimators', 50, 500),
        'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True),
        'max_depth':     trial.suggest_int('max_depth', 2, 8),
        'subsample':     trial.suggest_float('subsample', 0.5, 1.0),
    }
    model = GradientBoostingClassifier(**params, random_state=42)
    return cross_val_score(model, X, y, cv=5, scoring='accuracy').mean()

study = optuna.create_study(direction='maximize', sampler=optuna.samplers.TPESampler())
study.optimize(objective, n_trials=30, n_jobs=-1)

print(f'Best accuracy: {study.best_value:.4f}')
print(f'Best params:   {study.best_params}')

Use Cases

  • AutoML
  • model tuning
  • Bayesian optimization

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.