尝试使用BaysianOptimization使用多个参数进行绘图

时间:2018-07-29 15:46:53

标签: python matplotlib jupyter-notebook bayesian valueerror

我是python和贝叶斯优化的新手。我正在使用this example来查找“目标”多项式函数的最大值。该示例仅对函数使用一个“ x”参数,并生成如下图所示的图:
this

但是我想使用带有两个“ x”和“ z”参数的函数。添加第二个“ z”参数后,下面的代码似乎仍可以正确计算和识别最大值,但不会绘制结果,并产生此错误:

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

我一直在研究代码并搜索了几天,但仍然不知道为什么要这样做。有谁知道在“目标”函数中使用两个参数时如何绘制此代码?

我正在将Python 3.6.4与Anaconda和Jupyter结合使用。 这是我的代码:

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)

这是它产生的输出:

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 

~\Anaconda3\envs\base\lib\site-packages\sklearn\gaussian_process\gpr.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.

~\Anaconda3\envs\base\lib\site-packages\sklearn\gaussian_process\kernels.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:

~\Anaconda3\envs\base\lib\site-packages\scipy\spatial\distance.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.)

0 个答案:

没有答案