首页 > 解决方案 > 重塑 np.array 以防止错误:searn GPR XA 和 XB 必须具有相同的列数

问题描述

我想使用 scikit-learn 的高斯过程回归器。我用于预测和函数的数据,执行 gpr,如下所示:

训练数据:

X[[ 43.3301   -196.211    1157.89        2.71431  -191.664    1159.45
-28.9847   -191.544    1158.88        5.99311  -218.226    1229.12
 38.9442   -214.853    1256.5 ]
[ 43.3212   -196.12     1157.79        2.7885   -191.587    1159.45
-29.0067   -191.53     1158.88        5.94141  -218.028    1229.11
 38.8952   -214.711    1256.48]...] <class 'numpy.ndarray'> (14, 15)

目标数据:

y[[ 9.14779  -186.67     1294.53]
 [ 9.12453  -186.559    1294.33 ]
 [ 8.50554  -186.254    1293.22]...] <class 'numpy.ndarray'> (14, 3)

和测试数据

x[[ -90.23126221 -245.3088  675.835]
 [ -63.30067444 -256.1264  752.844]
 [ -28.84734154 -236.4492  642.7713]...] <class 'numpy.ndarray'> (14, 3)


def doGPR(self, inputData, targetData, testData):

    kernel = ConstantKernel(1.0, (1e-3,1e3)) * RBF(10.0, (1e-2,1e2)) + WhiteKernel(0.1, (1e-10, 0.5))
    gpr = GaussianProcessRegressor(kernel= kernel, n_restarts_optimizer= 0, normalize_y= True)  
    gpr.fit(X, y)
    predictedData, sigma = gpr.predict(x, return_std=True)

在预测阶段,我收到以下错误:

Traceback (most recent call last):
File "sklearn/gaussian_process/gpr.py", line 315, in predict
K_trans = self.kernel_(X, self.X_train_)
File "sklearn/gaussian_process/kernels.py", line 683, in __call__
return self.k1(X, Y) + self.k2(X, Y)
File "sklearn/gaussian_process/kernels.py", line 758, in __call__
return self.k1(X, Y) * self.k2(X, Y)
File "sklearn/gaussian_process/kernels.py", line 1215, in __call__
metric='sqeuclidean')
File "/usr/lib/python3/dist-packages/scipy/spatial/distance.py", line 2031, 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.)

这是否意味着我也应该将我的训练数据带入 shape (x, 3) ?我怎么能用这个嵌套数组做到这一点?

谢谢!

标签: pythonpython-3.xscikit-learn

解决方案


推荐阅读