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

问题描述 投票:2回答:1

我对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行,在test v1_pred = model.predict(x)文件“/usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/gpr.py”,第315行,in预测K_trans = self.kernel_(X,self.X_train_)文件“/usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/kernels.py”,第758行,在调用返回self.k1(X ,Y)* self.k2(X,Y)文件“/usr/local/lib/python3.6/site-packages/sklearn/gaussian_process/kernels.py”,第1215行,in call metric ='sqeuclidean')文件“/usr/local/lib/python3.6/site-packages/scipy/spatial/distance.py”,第2373行,在cdist中引发ValueError('XA和XB必须具有相同数量的列'ValueError:XA和XB必须具有相同数量的列(即要素尺寸。)

python pandas numpy machine-learning scikit-learn
1个回答
0
投票

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

X_train, v1 = make_regression()

只是不得不删除它。

© www.soinside.com 2019 - 2024. All rights reserved.