问题描述
发布之前,我进行了大量搜索,发现,这可能正是我的问题.但是,我尝试了答案中提出的内容,但是不幸的是,这并没有解决问题,并且由于我是这里的新成员,因此我无法添加评论以要求进一步的解释.
Prior to posting I did a lot of searches and found this question which might be exactly my problem. However, I tried what is proposed in the answer but unfortunately this did not fix it, and I couldn't add a comment to request further explanation, as I am a new member here.
无论如何,我想在一个简单但真实的案例中将高斯过程与scikit-learn一起使用在Python中(使用scikit-learn的文档中提供的示例).我有一个称为 X 的2D输入集(8对2个参数).我有8个对应的输出,收集在1D数组 y 中.
Anyway, I want to use the Gaussian Processes with scikit-learn in Python on a simple but real case to start (using the examples provided in scikit-learn's documentation). I have a 2D input set (8 couples of 2 parameters) called X. I have 8 corresponding outputs, gathered in the 1D-array y.
# Inputs: 8 points
X = np.array([[p1, q1],[p2, q2],[p3, q3],[p4, q4],[p5, q5],[p6, q6],[p7, q7],[p8, q8]])
# Observations: 8 couples
y = np.array([r1,r2,r3,r4,r5,r6,r7,r8])
我定义了一个输入测试空间 x :
I defined an input test space x:
# Input space
x1 = np.linspace(x1min, x1max) #p
x2 = np.linspace(x2min, x2max) #q
x = (np.array([x1, x2])).T
然后我实例化GP模型,使其适合我的训练数据(X,y),并在输入空间 x上进行一维预测 y_pred :
Then I instantiate the GP model, fit it to my training data (X,y), and make the 1D prediction y_pred on my input space x:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
kernel = C(1.0, (1e-3, 1e3)) * RBF([5,5], (1e-2, 1e2))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=15)
gp.fit(X, y)
y_pred, MSE = gp.predict(x, return_std=True)
然后我绘制3D图:
fig = pl.figure()
ax = fig.add_subplot(111, projection='3d')
Xp, Yp = np.meshgrid(x1, x2)
Zp = np.reshape(y_pred,50)
surf = ax.plot_surface(Xp, Yp, Zp, rstride=1, cstride=1, cmap=cm.jet,
linewidth=0, antialiased=False)
pl.show()
这是我得到的:
当我修改内核参数时,我得到了类似的内容,类似于我上面提到的海报:
When I modify the kernel parameters I get something like this, similar to what the poster I mentioned above got:
这些图甚至与原始训练点的观察结果都不匹配(对[65.1,37]的响应较低,而对[92.3,54]的响应最高).
These plots don't even match the observation from the original training points (the lower response is obtained for [65.1,37] and the highest for [92.3,54]).
我对2D的GP还是比较陌生的(不久前也开始使用Python),所以我认为我在这里缺少了一些东西……任何答案都将是有帮助的,非常感谢,谢谢!
I am fairly new to GPs in 2D (also started Python not long ago) so I think I'm missing something here... Any answer would be helpful and greatly appreciated, thanks!
推荐答案
您正在使用两个功能来预测第三个功能.而不是像plot_surface
这样的3D图,如果使用能够显示有关三维尺寸的信息(例如hist2d
或pcolormesh
)的2D图,通常会更清楚.这是一个使用与问题类似的数据/代码的完整示例:
You're using two features to predict a third. Rather than a 3D plot like plot_surface
, it's usually clearer if you use a 2D plot that's able to show information about a third dimension, like hist2d
or pcolormesh
. Here's a complete example using data/code similar to that in the question:
from itertools import product
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
X = np.array([[0,0],[2,0],[4,0],[6,0],[8,0],[10,0],[12,0],[14,0],[16,0],[0,2],
[2,2],[4,2],[6,2],[8,2],[10,2],[12,2],[14,2],[16,2]])
y = np.array([-54,-60,-62,-64,-66,-68,-70,-72,-74,-60,-62,-64,-66,
-68,-70,-72,-74,-76])
# Input space
x1 = np.linspace(X[:,0].min(), X[:,0].max()) #p
x2 = np.linspace(X[:,1].min(), X[:,1].max()) #q
x = (np.array([x1, x2])).T
kernel = C(1.0, (1e-3, 1e3)) * RBF([5,5], (1e-2, 1e2))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=15)
gp.fit(X, y)
x1x2 = np.array(list(product(x1, x2)))
y_pred, MSE = gp.predict(x1x2, return_std=True)
X0p, X1p = x1x2[:,0].reshape(50,50), x1x2[:,1].reshape(50,50)
Zp = np.reshape(y_pred,(50,50))
# alternative way to generate equivalent X0p, X1p, Zp
# X0p, X1p = np.meshgrid(x1, x2)
# Zp = [gp.predict([(X0p[i, j], X1p[i, j]) for i in range(X0p.shape[0])]) for j in range(X0p.shape[1])]
# Zp = np.array(Zp).T
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
ax.pcolormesh(X0p, X1p, Zp)
plt.show()
输出:
Kinda看起来很朴素,但是我的示例数据也是如此.通常,您不应该期望通过这几个数据点而获得特别有趣的结果.
Kinda plain looking, but so was my example data. In general, you shouldn't expect to get particular interesting resulting with this few data points.
此外,如果您确实想要表面图,则可以将pcolormesh
线替换为原来的(或多或少):
Also, if you do want the surface plot, you can just replace the pcolormesh
line with what you originally had (more or less):
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X0p, X1p, Zp, rstride=1, cstride=1, cmap='jet', linewidth=0, antialiased=False)
输出:
这篇关于如何正确使用scikit-learn的高斯过程进行2D输入,1D输出回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!