Bayesian Optimization
The bayesian-optimization library provides a Python implementation of the Bayesian Optimization (BO) algorithm, specifically designed for constrained global optimization of expensive black-box functions. It leverages Bayesian inference and Gaussian processes to efficiently find the maximum value of an unknown function with minimal evaluations. The current version is 3.2.1, and the project is actively maintained with a regular release cadence, requiring Python >=3.9.
Warnings
- gotcha The `BayesianOptimization` class is designed to find the *maximum* of the objective function by default. If your goal is to *minimize* a function `f(x)`, you should define your objective function to return `-f(x)`.
- gotcha This is a constrained optimization technique, meaning you must provide explicit upper and lower bounds for all parameters in the search space (`pbounds`). Failing to define bounds for any parameter will result in an error or incorrect behavior.
- gotcha Bayesian Optimization is most effective for computationally *expensive* black-box functions where each evaluation takes a significant amount of time (minutes or hours). Using it for very cheap, quickly computable functions may be less efficient than simpler optimization methods like grid search or random search due to the overhead of fitting the surrogate model.
- gotcha Standard Bayesian Optimization techniques, particularly those relying on Gaussian Processes, can struggle with high-dimensional search spaces. Performance tends to degrade significantly for problems with more than approximately 20 dimensions due to the 'curse of dimensionality' affecting the surrogate model.
Install
-
pip install bayesian-optimization
Imports
- BayesianOptimization
from bayes_opt import BayesianOptimization
Quickstart
from bayes_opt import BayesianOptimization
import os
def black_box_function(x, y):
"""Function with unknown internals we wish to maximize.
This is just serving as an example, for all intents and purposes think of
the internals of this function, i.e.: the process which generates its
output values, as unknown.
"""
# Example: A simple 2D quadratic function
return -x ** 2 - (y - 1) ** 2 + 1
# Bounded region of parameter space
pbounds = {'x': (0, 2), 'y': (0, 3)}
optimizer = BayesianOptimization(
f=black_box_function,
pbounds=pbounds,
random_state=1,
)
# Perform 2 initial random points and 5 iterations of Bayesian Optimization
optimizer.maximize(
init_points=2,
n_iter=5,
)
print(f"Best parameters found: {optimizer.max['params']}")
print(f"Maximum value found: {optimizer.max['target']}")