To perform Bayesian optimization, you must define a function to maximize and a dictionary of parameter bounds (pbounds).
- Define the function: The function
f must take a known set of parameters and return a real number. - Define bounds: Provide a dictionary where keys are parameter names and values are tuples representing the
(min, max) range. - Instantiate
BayesianOptimization: Pass the function, bounds, and an optional random_state. - Call
maximize: Use the maximize method to run the optimization process.
Key maximize parameters:
init_points: Number of initial random exploration steps.n_iter: Number of steps of bayesian optimization to perform.
from bayes_opt import BayesianOptimization
# 1. Define the function to be optimized
def black_box_function(x, y):
return -x ** 2 - (y - 1) ** 2 + 1
# 2. Define the bounded region of parameter space
pbounds = {'x': (2, 4), 'y': (-3, 3)}
# 3. Instantiate the optimizer
optimizer = BayesianOptimization(
f=black_box_function,
pbounds=pbounds,
random_state=1,
)
# 4. Run the optimization
optimizer.maximize(
init_points=2,
n_iter=3,
)