问题描述
我有一个要从Jupyter笔记本中运行的数据科学书中的示例.代码段看起来像这样
I have an example from a data science book I am trying to run in a Jupyter notebook. The code sippet looks like this
from sklearn.gaussian_process import GaussianProcess
# define the model and draw some data
model = lambda x: x * np.sin(x)
xdata = np.array([1, 3, 5, 6, 8])
ydata = model(xdata)
# Compute the Gaussian process fit
gp = GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1E-1,
random_start=100)
gp.fit(xdata[:, np.newaxis], ydata)
xfit = np.linspace(0, 10, 1000)
yfit, MSE = gp.predict(xfit[:, np.newaxis], eval_MSE=True)
dyfit = 2 * np.sqrt(MSE) # 2*sigma ~ 95% confidence region
因为GaussianProcess已被弃用并由GaussianProcessRegressor代替.我试图将代码段修复为这样
since GaussianProcess has been deprecated and replaced with GaussianProcessRegressor. I tried to fix the code snippet to look like this
from sklearn.gaussian_process import GaussianProcessRegressor
# define the model and draw some data
model = lambda x: x * np.sin(x)
xdata = np.array([1, 3, 5, 6, 8])
ydata = model(xdata)
# Compute the Gaussian process fit
gp = GaussianProcessRegressor(random_state=100)
gp.fit(xdata[:, np.newaxis], ydata)
xfit = np.linspace(0, 10, 1000)
yfit, MSE = gp.predict(xfit[:, np.newaxis])
dyfit = 2 * np.sqrt(MSE) # 2*sigma ~ 95% confidence region
但我收到ValueError
but I get a ValueError
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-47-c04ac57d1897> in <module>
11
12 xfit = np.linspace(0, 10, 1000)
---> 13 yfit, MSE = gp.predict(xfit[:, np.newaxis])
14 dyfit = 2 * np.sqrt(MSE) # 2*sigma ~ 95% confidence region
ValueError: too many values to unpack (expected 2)
不确定为什么预测函数会在这里抱怨吗?
bit unsure why the predict function complains here?
推荐答案
错误有答案.
在yfit, MSE = gp.predict(xfit[:, np.newaxis])
中,您尝试将预测结果分配给两个变量,而预测只返回单个numpy.ndarray
.
At yfit, MSE = gp.predict(xfit[:, np.newaxis])
you are trying to assign the result of predict to two variables while the predict only returns a single numpy.ndarray
.
要解决此问题,请运行
yfit = gp.predict(xfit[:, np.newaxis])
这篇关于修改旧的GaussianProcessor示例以与GaussianProcessRegressor一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!