Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Python Bayesian Optimization Sample Code


Python Bayesian Optimization Sample Code


Here is a sample code for Bayesian Optimization using Python:

python
import numpy as np
from bayesian_optimization import BayesianOptimization

# Define the objective function
def objective_function(x):
    return -x[0] ** 2 - (x[1] - 1) ** 2 + 1

# Define the bounds for the parameters
bounds = {'x': (0, 10), 'y': (0, 10)}

# Initialize the Bayesian Optimization
optimizer = BayesianOptimization(
    f=objective_function,
    pbounds=bounds,
    verbose=2
)

# Run the optimization process
optimizer.maximize(init_points=5, n_iter=20)

# Print the best found parameters and the corresponding function value
print(optimizer.max)

This code uses the `bayesian-optimization` library to perform Bayesian Optimization on a simple function. The objective function is defined as a black-box function with unknown internals, and the bounds for the parameters are specified. The optimization process is initialized with 5 random points and runs for 20 iterations. The best found parameters and the corresponding function value are printed at the end[1][2].

For a more detailed example, you can use the `bayesian-optimization` library to optimize the hyperparameters of a Gaussian Process Regressor. Here is a sample code:

python
from bayes_opt import BayesianOptimization
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C

# Define the objective function
def objective_function(C, RBF_length_scale):
    kernel = C * RBF(RBF_length_scale)
    gp = GaussianProcessRegressor(kernel=kernel)
    gp.fit(X_train, y_train)
    predictions = gp.predict(X_test)
    return -mean_squared_error(y_test, predictions)

# Define the bounds for the hyperparameters
bounds = {'C': (1e-3, 1e3), 'RBF_length_scale': (1e-2, 1e2)}

# Initialize the Bayesian Optimization
optimizer = BayesianOptimization(
    f=objective_function,
    pbounds=bounds,
    verbose=2
)

# Run the optimization process
optimizer.maximize(init_points=5, n_iter=20)

# Print the best found hyperparameters and the corresponding function value
print(optimizer.max)

This code uses the `bayesian-optimization` library to optimize the hyperparameters of a Gaussian Process Regressor. The objective function is defined as the negative mean squared error between the predicted and actual values. The bounds for the hyperparameters are specified, and the optimization process is initialized with 5 random points and runs for 20 iterations. The best found hyperparameters and the corresponding function value are printed at the end[2][4].

For a more advanced example, you can use the `bayesian-optimization` library to optimize the hyperparameters of a support vector machine. Here is a sample code:

python
from bayes_opt import BayesianOptimization
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

# Define the objective function
def objective_function(C, gamma):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    model = SVC(C=C, gamma=gamma)
    model.fit(X_train, y_train)
    return -model.score(X_test, y_test)

# Define the bounds for the hyperparameters
bounds = {'C': (1e-3, 1e3), 'gamma': (1e-2, 1e2)}

# Initialize the Bayesian Optimization
optimizer = BayesianOptimization(
    f=objective_function,
    pbounds=bounds,
    verbose=2
)

# Run the optimization process
optimizer.maximize(init_points=5, n_iter=20)

# Print the best found hyperparameters and the corresponding function value
print(optimizer.max)

This code uses the `bayesian-optimization` library to optimize the hyperparameters of a support vector machine. The objective function is defined as the negative accuracy of the model. The bounds for the hyperparameters are specified, and the optimization process is initialized with 5 random points and runs for 20 iterations. The best found hyperparameters and the corresponding function value are printed at the end[2][4].

These examples demonstrate how Bayesian Optimization can be used to optimize various types of objective functions and hyperparameters in Python.

Citations:
[1] https://github.com/bayesian-optimization/BayesianOptimization
[2] https://drlee.io/bayesian-optimization-with-python-b544255757d3?gi=55350ad53394
[3] https://machinelearningmastery.com/what-is-bayesian-optimization/
[4] https://github.com/thuijskens/bayesian-optimization
[5] https://pyro.ai/examples/bo.html