The top three commonly used Python libraries for Bayesian optimization are:

Scikit-Optimize (skopt)
GPyOpt
BayesianOptimization
Scikit-Optimize (skopt) is the most highly recommended library due to its integration with the popular Scikit-learn library and its ease of use. Here's a basic example of implementing Bayesian optimization using Scikit-Optimize with a Gaussian process surrogate model and an expected improvement acquisition function:

import numpy as np
from skopt import gp_minimize
from skopt.space import Real
from skopt.utils import use_named_args

# Define the objective function
def objective_function(x):
    return (np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2)) * np.random.randn() * 0.1)

# Define the search space for the optimization
search_space = [Real(-2, 2, name="x")]

# Define the optimization function with named arguments
@use_named_args(search_space)
def optimization_function(**params):
    x = params["x"]
    return objective_function([x])

# Perform Bayesian optimization using a Gaussian process surrogate model and expected improvement acquisition function
result = gp_minimize(optimization_function, search_space, n_calls=20, n_random_starts=5, noise=0.1, random_state=0, acq_func="EI")

# Print the optimal parameters and the minimum value of the objective function
print("Optimal parameters:", result.x)
print("Minimum value of the objective function:", result.fun)

In this example, we first import the necessary libraries and define the objective function to be optimized. We then define the search space for the optimization as a single continuous variable named "x" with a range of [-2, 2]. The optimization_function is defined using the use_named_args decorator, which allows the function to accept parameters as keyword arguments.

The gp_minimize function is used to perform the Bayesian optimization, specifying the Gaussian process surrogate model (the default), the expected improvement acquisition function (acq_func="EI"), and the number of function evaluations (n_calls) and random starts (n_random_starts). The optimization result contains the optimal parameters and the minimum value of the objective function, which are printed at the end.
spam fails_task lang_mismatch pii not_appropriate hate_speech sexual_content quality toxicity humor helpfulness creativity violence