XGBoosting Home | About | Contact | Examples

Bayesian Optimization of XGBoost Hyperparameters with bayes_opt

Bayesian optimization is a powerful and efficient approach for tuning the hyperparameters of machine learning models like XGBoost.

Compared to traditional methods like grid search, which exhaustively evaluates all combinations of hyperparameters, Bayesian optimization intelligently selects the next set of hyperparameters to evaluate based on the results of previous evaluations.

This can lead to finding better hyperparameters in fewer iterations, especially for high-dimensional search spaces.

While libraries like scikit-optimize provide Bayesian optimization capabilities, the bayesian-optimization library or bayes_opt for short, offers a lightweight and flexible alternative that can be easily integrated with XGBoost. Here’s an example of how to use bayes_opt` to optimize XGBoost hyperparameters:

First, install the bayes_opt library using your preferred Python package manager, such as pip:

pip install bayesian-optimization

Then, use bayes_opt to define the search space and optimize the hyperparameters:

import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from bayes_opt import BayesianOptimization

# Generate synthetic regression dataset
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define the objective function to minimize
def xgb_eval(max_depth, learning_rate, subsample, colsample_bytree):
    params = {
        'max_depth': int(max_depth),
        'learning_rate': learning_rate,
        'subsample': subsample,
        'colsample_bytree': colsample_bytree,
        'n_estimators': 100,
        'objective': 'reg:squarederror',
        'random_state': 42,
    }

    model = XGBRegressor(**params)
    model.fit(X_train, y_train)
    score = model.score(X_test, y_test)
    return score

# Define the search space
pbounds = {
    'max_depth': (3, 10),
    'learning_rate': (0.01, 0.3),
    'subsample': (0.5, 1.0),
    'colsample_bytree': (0.5, 1.0),
}

# Perform Bayesian optimization
optimizer = BayesianOptimization(f=xgb_eval, pbounds=pbounds, random_state=42)
optimizer.maximize(init_points=5, n_iter=25)

# Print the best hyperparameters and score
print(f"Best hyperparameters: {optimizer.max['params']}")
print(f"Best score: {optimizer.max['target']:.4f}")

In this example:

  1. We generate a synthetic regression dataset using scikit-learn’s make_regression function and split it into train and test sets.

  2. We define an objective function xgb_eval that takes hyperparameters as input, creates an XGBoost model with those hyperparameters, fits it on the training data, and returns the model’s score on the test data.

  3. We define the search space pbounds using tuples to specify the ranges for each hyperparameter.

  4. We create a BayesianOptimization object, specifying the objective function and search space. We then call maximize to perform the optimization, specifying the number of initial random points (init_points) and the number of iterations (n_iter).

  5. After optimization, we print the best hyperparameters and the corresponding best score.

By leveraging Bayesian optimization with BayesOpt, we can efficiently find high-performing hyperparameters for XGBoost, potentially saving significant computational resources compared to exhaustive search methods. This approach is particularly valuable when dealing with large datasets and complex models where training times are substantial.



See Also