Modifying old GaussianProcessor example to run with GaussianProcessRegressor

时光总嘲笑我的痴心妄想 提交于 2021-01-07 08:57:08

问题


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

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

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?


回答1:


The error has the answer.

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.

To solve this issue, run

yfit = gp.predict(xfit[:, np.newaxis])



来源:https://stackoverflow.com/questions/57221972/modifying-old-gaussianprocessor-example-to-run-with-gaussianprocessregressor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!