XGBoosting Home | About | Contact | Examples

Bayesian Optimization of XGBoost Hyperparameters with optuna

Optuna is a popular Python library for hyperparameter optimization that can be used to tune the hyperparameters of machine learning models like XGBoost.

It employs an efficient approach called Bayesian optimization, which intelligently selects the most promising hyperparameters to evaluate based on previous results.

Under the hood, Optuna constructs a probabilistic model of the objective function, uses this model to suggest hyperparameters that are likely to yield good performance, evaluates the model with these hyperparameters, and then updates the probabilistic model based on the evaluation results. This process is repeated iteratively, allowing Optuna to hone in on the optimal hyperparameters.

By leveraging Bayesian optimization, Optuna can often find better hyperparameters in fewer iterations compared to methods like random search or grid search. This efficiency can save significant computational resources, especially when working with large datasets and complex models.

Here’s an example of how to use Optuna to optimize the hyperparameters of an XGBoost classifier:

First, install Optuna using pip:

pip install optuna

Then, define the objective function and run the optimization:

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from xgboost import XGBClassifier
import optuna

# Generate a synthetic classification dataset
X, y = make_classification(n_samples=1000, n_classes=2, n_informative=10, random_state=42)

def objective(trial):
    # Suggest hyperparameters
    params = {
        'max_depth': trial.suggest_int('max_depth', 2, 10),
        'learning_rate': trial.suggest_float('learning_rate', 1e-3, 1.0, log=True),
        'subsample': trial.suggest_float('subsample', 0.5, 1.0),
        'colsample_bytree': trial.suggest_float('colsample_bytree', 0.5, 1.0),

    # Create an XGBoost classifier with the suggested hyperparameters
    model = XGBClassifier(**params, n_estimators=100, objective='binary:logistic', random_state=42)

    # Perform 5-fold cross-validation and return the mean accuracy
    scores = cross_val_score(model, X, y, cv=5, scoring='accuracy')
    return scores.mean()

# Create an Optuna study with TPE sampler
study = optuna.create_study(direction='maximize', sampler=optuna.samplers.TPESampler())

# Optimize the study for 100 trials
study.optimize(objective, n_trials=100)

# Print the best hyperparameters and best score
print(f"Best hyperparameters: {study.best_params}")
print(f"Best score: {study.best_value:.4f}")

In this example, we:

  1. Generate a synthetic binary classification dataset using sklearn’s make_classification function.

  2. Define an objective function that takes a trial object as input. This function suggests hyperparameters using Optuna’s suggest_* methods, creates an XGBClassifier with these hyperparameters, performs 5-fold cross-validation using cross_val_score, and returns the mean accuracy.

  3. Create an Optuna study with a Tree-structured Parzen Estimator (TPE) sampler, which is a Bayesian optimization algorithm.

  4. Optimize the study for 100 trials by calling study.optimize() with the objective function and the number of trials.

  5. Print the best hyperparameters and the corresponding best score found by Optuna.

By using Optuna’s Bayesian optimization, you can efficiently tune XGBoost’s hyperparameters to achieve high performance on your classification task. This approach is applicable to a wide range of machine learning problems and can help you get the most out of your XGBoost models.

See Also