Skip to main content
 首页 » 编程设计

python之Sklearn,高斯过程 : XA and XB must have the same number of columns

2025年01月19日10yyy_WW

我对 python 很陌生,对做高斯回归很感兴趣。 我在 py3.6 和 SKlearn 0.19 下。

我有简单的代码,但我得到了一个关于预测调用的 cdist 中向量维度的错误。我知道我的输入有问题。但我不明白为什么...

我找了高斯过程回归器的例子,但它似乎不是最常用的工具。

预先感谢您的帮助。

干杯。

这是我的代码示例:

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) 

运行时出现如下错误:

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() 

只好删除它。