Sklearn,Gaussian过程:XA和XB必须具有相同的列数

时间:2018-03-02 11:52:29

标签: python pandas numpy machine-learning scikit-learn

我对python很新,并且对高斯回归感兴趣。 我在py3.6和SKlearn 0.19下。

我有简单的代码,我得到一个关于cdist中的矢量维度的错误,由predict调用。我理解我的输入有些不好。但我不明白为什么......

我查看了高斯过程回归的示例,但它似乎不是最常用的工具。

提前感谢您的帮助。

干杯。

以下是我的代码示例:

import pandas as pd
import numpy as np
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor as gpr
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C

....

#X_train are the training samples
X_train= np.column_stack((xc,yc,zc))
print('X_train')
print(X_train.shape)
print(X_train)

这是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.0
1       14.0
2       13.0
3       19.0
....
4573    39.0
4574    16.0
4575    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()
#default kernel
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)

#Predict v1 
v1_pred = model.predict(x)

运行时出现以下错误:

  

文件" test.py",第189行,在测试中       v1_pred = model.predict(x)File" /usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/gpr.py",   第315行,预测       K_trans = self.kernel_(X,self.X_train_)File" /usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/kernels.py",   第758行,致电       return self.k1(X,Y)* self.k2(X,Y)File" /usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/kernels.py",   第1215行,致电       metric =' sqeuclidean')文件" /usr/local/lib/python3.6/site-packages/scipy/spatial/distance.py",   第2373行,在cdist中       提高ValueError(' XA和XB必须具有相同的列数' ValueError:XA和XB必须具有相同的列数(即   特征维度。)

1 个答案:

答案 0 :(得分:0)

我只是复制粘贴代码并做了一些愚蠢的事情:

X_train, v1 = make_regression()

只需将其删除即可。