我正在Python中运行高斯回归。我的数据集的形状为(10000,5)。但是,当我尝试拟合模型时,会出现错误:
AttributeError: 'list' object has no attribute 'n_dims'
我该如何解决?
我最初以为是由于我的因变量的尺寸可能与自变量不同而引起的。但是,即使将它们更改为相同的尺寸,我也无法找到代码的问题。任何帮助都感激不尽。
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import (RBF, Matern, RationalQuadratic,
ExpSineSquared, DotProduct,
ConstantKernel)
data_set = pd.read_excel(r'XXXXX', sheet = 'Worksheet', header = 0)
data_set.head()
test_set = data_set
y = test_set.iloc[:,4]
test_set.drop(test_set.columns[4], axis = 1, inplace = True)
X = test_set
x=StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
y_train = np.array(y_train)
y_test = np.array(y_test)
y_train = np.reshape(y_train, (7000,1))
y_test = np.reshape(y_test, (3000,1))
kernels = [1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))]
gp = GaussianProcessRegressor(kernel=kernels)
gp.fit(X_train, y_train)
File "<ipython-input-23-5a576449fdb6>", line 1, in <module>
gp.fit(X_train, y_train)
File "C:\Program Files\Anaconda\lib\site-packages\sklearn\gaussian_process\gpr.py", line 203, in fit
if self.optimizer is not None and self.kernel_.n_dims > 0:
AttributeError: 'list' object has no attribute 'n_dims'
答案 0 :(得分:0)
初始化GaussianProcessRegressor(kernel=kernels)
时,以kernel
传递的参数必须是内核对象。您正在传递列表。
文档here中的更多信息。