In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from bayes_opt import BayesianOptimization
# use sklearn's default parameters for theta and random_start
gp_params = {"corr": "cubic", "theta0": 0.1, "thetaL": None, "thetaU": None, "random_start": 1}
In [2]:
np.random.seed(42)
xs = np.linspace(-2, 10, 10000)
f = np.exp(-(xs - 2)**2) + np.exp(-(xs - 6)**2/10) + 1/ (xs**2 + 1)
plt.plot(f)
plt.show()
In [3]:
def plot_bo(f, bo):
xs = [x["x"] for x in bo.res["all"]["params"]]
ys = bo.res["all"]["values"]
mean, var = bo.gp.predict(np.arange(len(f)).reshape(-1, 1), eval_MSE=True)
plt.figure(figsize=(16, 9))
plt.plot(f)
plt.plot(np.arange(len(f)), mean)
plt.fill_between(np.arange(len(f)), mean+var, mean-var, alpha=0.1)
plt.scatter(bo.X.flatten(), bo.Y, c="red", s=50, zorder=10)
plt.xlim(0, len(f))
plt.ylim(f.min()-0.1*(f.max()-f.min()), f.max()+0.1*(f.max()-f.min()))
plt.show()
In [4]:
bo = BayesianOptimization(f=lambda x: f[int(x)],
pbounds={"x": (0, len(f)-1)},
verbose=0)
bo.maximize(init_points=2, n_iter=25, acq="ei", xi=0.0, **gp_params)
plot_bo(f, bo)
In [5]:
bo = BayesianOptimization(f=lambda x: f[int(x)], pbounds={"x": (0, len(f)-1)}, verbose=0)
bo.maximize(init_points=2, n_iter=25, acq="ei", xi=0.1, **gp_params)
plot_bo(f, bo)
In [6]:
bo = BayesianOptimization(f=lambda x: f[int(x)],
pbounds={"x": (0, len(f)-1)},
verbose=0)
bo.maximize(init_points=2, n_iter=25, acq="poi", xi=0.0, **gp_params)
plot_bo(f, bo)
In [7]:
bo = BayesianOptimization(f=lambda x: f[int(x)], pbounds={"x": (0, len(f)-1)}, verbose=0)
bo.maximize(init_points=2, n_iter=25, acq="poi", xi=0.1, **gp_params)
plot_bo(f, bo)
In [ ]: