我对Python还比较新手,并且对进行高斯回归很感兴趣。我使用的是Python 3.6 和 SKlearn 0.19版本。
我有一段简单的代码,但我在运行时遇到了关于cdist函数中向量维度的问题,该函数是由predict调用的。我明白我的输入有问题,但我不明白为什么…
我查找了一些高斯过程回归器的示例,但似乎这并不是最常用的工具。
提前感谢您的帮助。
祝好。
这是我代码的一个样本:
这是X_train的输出:
X_train (4576, 3) [[ 0.71958336 -1.12719598 0.47889958] [ 0.71958336 -1.12719598 0.47889958] [ 0.71958336 -1.12719598 0.34285071] ... [ 0.55255508 -1.18817547 -1.63666023] [ 0.55255508 -1.18817547 -1.70468466] [ 0.55255508 -1.18817547 -1.77270909]]
这是训练目标特征:
print('v1')print(v1.shape)print(v1)
其输出:
v1(4576,)0 10.01 14.02 13.03 19.0....4573 39.04574 16.04575 12.0
这是要预测的样本:
x = np.column_stack((xp, yp, zp))print('x')print(x.shape)print(x)
其输出:
x(75, 3)[[-1.41421356 -1.41421356 -1.22474487] [-0.70710678 -1.41421356 -1.22474487] [ 0. -1.41421356 -1.22474487] [ 0.70710678 -1.41421356 -1.22474487]..... [ 0.70710678 -0.70710678 -1.22474487] [ 1.41421356 -0.70710678 -1.22474487] [-1.41421356 0. -1.22474487] [-0.70710678 0. -1.22474487] [ 0. 0. -1.22474487]
这是拟合和预测的过程:
v1 = v1.ravel()#默认内核kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))X_train, v1 = make_regression()model = gpr(kernel=kernel, n_restarts_optimizer=9)model.fit(X_train,v1)#预测v1 v1_pred = model.predict(x)
运行时我得到了以下错误:
File “test.py”, line 189, in test v1_pred = model.predict(x) File “/usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/gpr.py”, line 315, in predict K_trans = self.kernel_(X, self.X_train_) File “/usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/kernels.py”, line 758, in call return self.k1(X, Y) * self.k2(X, Y) File “/usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/kernels.py”, line 1215, in call metric=’sqeuclidean’) File “/usr/local/lib/python3.6/site-packages/scipy/spatial/distance.py”, line 2373, in cdist raise ValueError(‘XA and XB must have the same number of columns ‘ ValueError: XA and XB must have the same number of columns (i.e. feature dimension.)
回答:
我只是复制粘贴了一段代码,并且做了一些愚蠢的事情:
X_train, v1 = make_regression()
只需删除它即可。