0

我开始学习 python 并尝试使用 Sklearn 库实现高斯回归。对于我自己的数据点,我尝试按照此处提供的示例进行操作。但是,当我尝试运行y_pred, std = model.predict(X_te, return_std=True)我的问题的这行代码时,我得到了以下示例。我得到的错误“ XA 和 XB 必须具有相同的列数(即特征维度。)”

我不知道我在哪里犯了错误,请帮助并提前感谢。

输入输出数据示例如下

X_tr= [10.8204  7.67418 7.83013 8.30996 8.1567  6.94831 14.8673 7.69338 7.67702 12.7542 11.847] 
y_tr= [1965.21  854.386 909.126 1094.06 1012.6  607.299 2294.55 866.316 822.948 2255.32 2124.67]
X_te= [7.62022  13.1943 7.76752 8.36949 7.86459 7.16032 12.7035 8.99822 6.32853 9.22345 11.4751]

X_tr, y_tr并且X_te是训练数据点并且是重塑值并且具有“float64数组”类型

这是我的代码示例:

import sklearn.gaussian_process as gp

kernel = gp.kernels.ConstantKernel(1.0, (1e-1, 1e3)) * gp.kernels.RBF(10.0, (1e-3, 1e3))

model = gp.GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10, alpha=0.1, normalize_y=True)

# data reshape
X_tr = X_tr.values.reshape(1,-1)
y_tr = y_tr.values.reshape(1,-1)

model.fit(X_tr, y_tr)
params = model.kernel_.get_params()

X_te = X_te.values.reshape(1,-1)

y_pred, std = model.predict(X_te, return_std=True)
4

1 回答 1

0

这行得通。我将您的数据从更改pandasnumpy数组并修复了reshape导致错误的问题。

import numpy as np

X_tr= np.array([10.8204,  7.67418, 7.83013, 8.30996, 8.1567,  6.94831, 14.8673, 7.69338, 7.67702, 12.7542, 11.847])
y_tr= np.array([1965.21,  854.386, 909.126, 1094.06, 1012.6,  607.299, 2294.55, 866.316, 822.948, 2255.32, 2124.67])
X_te= np.array([7.62022, 13.1943, 7.76752, 8.36949, 7.86459, 7.16032, 12.7035, 8.99822, 6.32853, 9.22345, 11.4751])

import sklearn.gaussian_process as gp

kernel = gp.kernels.ConstantKernel(1.0, (1e-1, 1e3)) * gp.kernels.RBF(10.0, (1e-3, 1e3))

model = gp.GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10, alpha=0.1, normalize_y=True)

# data reshape
X_tr = X_tr.reshape(-1,1)
y_tr = y_tr

model.fit(X_tr, y_tr)
params = model.kernel_.get_params()

X_te = X_te.reshape(-1,1)

y_pred, std = model.predict(X_te, return_std=True)
于 2020-07-05T14:30:20.237 回答