Sure, here are the top three commonly used Python libraries for Bayesian optimization:
scikit-optimize: scikit-optimize is a popular open-source library for Bayesian optimization that provides a simple interface for optimizing a wide range of functions. It includes several popular surrogate models and acquisition functions, including Gaussian process and expected improvement.
Hyperopt: Hyperopt is another widely used open-source library for Bayesian optimization that is particularly popular for optimizing machine learning models. It includes several powerful search algorithms and supports both sequential and parallel optimization.
GPyOpt: GPyOpt is a popular library for Bayesian optimization that is built on top of the GPy library for Gaussian processes. It includes several state-of-the-art surrogate models and acquisition functions, and is particularly well-suited for optimizing complex functions with a large number of inputs.
For the most highly recommended library, scikit-optimize, here is an example of how to implement Bayesian optimization using a Gaussian process surrogate model and an expected improvement acquisition function:
from skopt import gp_minimize
from skopt import Optimizer
from skopt.space import Real
from skopt.utils import use_named_args
from skopt.plots import plot_convergence
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
import numpy as np
Define the function to be optimized
def f(x):
return np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2)) + np.random.randn() * 0.1
Define the search space for the optimizer
space = [Real(-2, 2, name='x')]
Create a Gaussian process surrogate model with a Matern kernel
gp = GaussianProcessRegressor(kernel=Matern(length_scale=0.1, nu=1.5))
Create an optimizer with an expected improvement acquisition function
opt = Optimizer(
dimensions=space,
base_estimator=gp,
acq_func='EI',
n_initial_points=10,
random_state=42
)
Define the objective function to be used for optimization
@use_named_args(space)
def objective(**params):
return -f([params['x']])
Optimize the function using Bayesian optimization
result = gp_minimize(
func=objective,
dimensions=space,
n_calls=50,
acq_func='EI',
x0=[0],
y0=None,
random_state=42,
verbose=True
)
Print the best result
print("Best result: x=%.3f, f(x)=%.3f" % (result.x[0], result.fun))
Plot the convergence of the optimization process
plot_convergence(result)
In this example, we define a simple one-dimensional function to be optimized and create a Gaussian process surrogate model with a Matern kernel. We then create an optimizer with an expected improvement acquisition function and define the objective function to be used for optimization.
Finally, we use the gp_minimize function from scikit-optimize to perform the Bayesian optimization and obtain the best result. We also plot the convergence of the optimization process using the plot_convergence function.