Trying to plot with more than one parameter using BaysianOptimization

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Trying to plot with more than one parameter using BaysianOptimization



I am new to python and Bayesian Optimization. I'm using this example to find the maximum value of the "target" polynomial function. The example uses only one "x" parameter for the function and produces a plot that looks like this:
this.



But I want to use a function with two "x" and "z" parameters. After adding a second "z" parameter, the code (below) appears to still calculate and identify the max correctly, but does not plot the results, and produces this error:


ValueError: XA and XB must have the same number of columns (i.e. feature dimension.)



I've been playing with the code and searching for days, but still haven't figured out why it's doing this. Does anyone know how to get this code to plot when using two parameters in the "target" function?



I'm using Python 3.6.4 with Anaconda and Jupyter.
Here is my code:


from bayes_opt import BayesianOptimization
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
%matplotlib inline

def target(x,z):
return -x ** 2 - (z - 1) ** 2 + 1

x = np.linspace(-2, 10, 10000).reshape(-1, 1)
z = np.linspace(-2, 10, 10000).reshape(-1, 1)
y = target(x,z)

bo = BayesianOptimization(target, {'x': (-4, 4), 'z': (-4, 4)})
bo.maximize(init_points=2, n_iter=10, acq='ucb', kappa=2)

print(bo.res['max'])

# Plot
def posterior(bo, x, xmin=-4, xmax=4):
xmin, xmax = -4, 4
bo.gp.fit(bo.X, bo.Y)
mu, sigma = bo.gp.predict(x, return_std=True)
return mu, sigma

def plot_gp(bo, x, y):

fig = plt.figure(figsize=(16, 10))
fig.suptitle('Gaussian Process and Utility Function After {} Steps'.format(len(bo.X)), fontdict={'size':30})

gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
axis = plt.subplot(gs[0])
acq = plt.subplot(gs[1])

mu, sigma = posterior(bo, x)
axis.plot(x, y, linewidth=3, label='Target')
axis.plot(bo.X.flatten(), bo.Y, 'D', markersize=8, label=u'Observations', color='r')
axis.plot(x, mu, '--', color='k', label='Prediction')

axis.fill(np.concatenate([x, x[::-1]]),
np.concatenate([mu - 1.9600 * sigma, (mu + 1.9600 * sigma)[::-1]]),
alpha=.6, fc='c', ec='None', label='95% confidence interval')

axis.set_xlim((-2, 10))
axis.set_ylim((None, None))
axis.set_ylabel('f(x)', fontdict={'size':20})
axis.set_xlabel('x', fontdict={'size':20})

utility = bo.util.utility(x, bo.gp, 0)
acq.plot(x, utility, label='Utility Function', color='purple')
acq.plot(x[np.argmax(utility)], np.max(utility), '*', markersize=15,
label=u'Next Best Guess', markerfacecolor='gold', markeredgecolor='k', markeredgewidth=1)
acq.set_xlim((-2, 10))
acq.set_ylim((0, np.max(utility) + 0.5))
acq.set_ylabel('Utility', fontdict={'size':20})
acq.set_xlabel('x', fontdict={'size':20})

axis.legend(loc=2, bbox_to_anchor=(1.01, 1), borderaxespad=0.)
acq.legend(loc=2, bbox_to_anchor=(1.01, 1), borderaxespad=0.)

plot_gp(bo, x, y)



And here is the output it produces:


Initialization
-----------------------------------------------------
Step | Time | Value | x | z |
1 | 00m00s | 0.94000 | -0.1787 | 1.1675 |
2 | 00m00s | -12.34014 | -2.2878 | 3.8471 |
Bayesian Optimization
-----------------------------------------------------
Step | Time | Value | x | z |
3 | 00m00s | 0.93129 | -0.1481 | 1.2163 |
4 | 00m02s | 0.25567 | 0.7278 | 0.5367 |
5 | 00m02s | -1.50156 | -1.0519 | -0.1811 |
6 | 00m02s | -1.30715 | 1.1340 | 2.0105 |
7 | 00m01s | 0.06311 | -0.7825 | 1.5698 |
8 | 00m01s | -40.00000 | 4.0000 | -4.0000 |
9 | 00m01s | -40.00000 | -4.0000 | -4.0000 |
10 | 00m01s | -24.00000 | 4.0000 | 4.0000 |
11 | 00m02s | 0.97319 | 0.1637 | 0.9983 |
12 | 00m02s | 0.96320 | -0.0144 | 0.8087 |
{'max_val': 0.9731873081299078, 'max_params': {'x': 0.16373702549648816, 'z': 0.9983034294431097}}

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-22ccc90ad234> in <module>()
59 acq.legend(loc=2, bbox_to_anchor=(1.01, 1), borderaxespad=0.)
60
---> 61 plot_gp(bo, x, y)

<ipython-input-20-22ccc90ad234> in plot_gp(bo, x, y)
33 acq = plt.subplot(gs[1])
34
---> 35 mu, sigma = posterior(bo, x)
36 axis.plot(x, y, linewidth=3, label='Target')
37 axis.plot(bo.X.flatten(), bo.Y, 'D', markersize=8, label=u'Observations', color='r')

<ipython-input-20-22ccc90ad234> in posterior(bo, x, xmin, xmax)
21 xmin, xmax = -4, 4
22 bo.gp.fit(bo.X, bo.Y)
---> 23 mu, sigma = bo.gp.predict(x, return_std=True)
24 return mu, sigma
25

~Anaconda3envsbaselibsite-packagessklearngaussian_processgpr.py in predict(self, X, return_std, return_cov)
313 return y_mean
314 else: # Predict based on GP posterior
--> 315 K_trans = self.kernel_(X, self.X_train_)
316 y_mean = K_trans.dot(self.alpha_) # Line 4 (y_mean = f_star)
317 y_mean = self._y_train_mean + y_mean # undo normal.

~Anaconda3envsbaselibsite-packagessklearngaussian_processkernels.py in __call__(self, X, Y, eval_gradient)
1322 "Gradient can only be evaluated when Y is None.")
1323 dists = cdist(X / length_scale, Y / length_scale,
-> 1324 metric='euclidean')
1325
1326 if self.nu == 0.5:

~Anaconda3envsbaselibsite-packagesscipyspatialdistance.py in cdist(XA, XB, metric, *args, **kwargs)
2371 raise ValueError('XB must be a 2-dimensional array.')
2372 if s[1] != sB[1]:
-> 2373 raise ValueError('XA and XB must have the same number of columns '
2374 '(i.e. feature dimension.)')
2375

ValueError: XA and XB must have the same number of columns (i.e. feature dimension.)









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty