【发布时间】:2023-03-22 00:00:02
【问题描述】:
我正在尝试使用 GaussianProcessRegressor 拟合 GP,但我注意到我的超参数仍处于初始值。我在 gpr.py 中做了一些步骤,但无法确定确切的原因。使用初始值进行预测会产生一条零线。
我的数据包含 5400 个样本,每个样本有 12 个特征,映射到单个输出变量。尽管设计可能不是那么好,但我仍然希望能学到一些东西。
所需文件:
import pandas as pd
import numpy as np
import time
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel,WhiteKernel
designmatrix = pd.read_csv('features.txt', index_col = 0)
y = pd.read_csv('output.txt', header=None, index_col = 0)
# The RBF kernel is a stationary kernel. It is also known as the “squared exponential” kernel.
# It is parameterized by a length-scale parameter length_scale>0, which can either be a scalar (isotropic variant of the kernel)
# or a vector with the same number of dimensions as the inputs X (anisotropic variant of the kernel).
#
# The ConstantKernel can be used as part of a product-kernel where it scales the magnitude of the other factor (kernel) or as
# part of a sum-kernel, where it modifies the mean of the Gaussian process.
#
# The main use-case of the White kernel is as part of a sum-kernel where it explains the noise-component of the signal.
# Tuning its parameter corresponds to estimating the noise-level: k(x_1, x_2) = noise_level if x_1 == x_2 else 0
kernel = ConstantKernel(0.1, (1e-23, 1e5)) *
RBF(0.1*np.ones(designmatrix.shape[1]), (1e-23, 1e10) ) + WhiteKernel(0.1, (1e-23, 1e5))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=0)
print('Training')
t = time.time()
gp = gp.fit(designmatrix, y)
elapsed = time.time() - t
print(elapsed)
score = gp.score(designmatrix, y)
print(score)
print("initial params")
params = gp.get_params()
print(params)
print("learned kernel params")
print(gp.kernel_.get_params())
结果如下:
initial params
{'alpha': 1e-10, 'copy_X_train': True, 'kernel__k1': 1**2, 'kernel__k2': RBF(len
gth_scale=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 'kernel__k1__constant_value': 1
.0, 'kernel__k1__constant_value_bounds': (1e-05, 100000.0), 'kernel__k2__length_
scale': array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]), 'ke
rnel__k2__length_scale_bounds': (1e-05, 100000.0), 'kernel': 1**2 * RBF(length_s
cale=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 'n_restarts_optimizer': 0, 'normaliz
e_y': False, 'optimizer': 'fmin_l_bfgs_b', 'random_state': None}
learned kernel params
{'k1': 1**2, 'k2': RBF(length_scale=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 'k1__
constant_value': 1.0, 'k1__constant_value_bounds': (1e-05, 100000.0), 'k2__lengt
h_scale': array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]), '
k2__length_scale_bounds': (1e-05, 100000.0)}
所以,内核参数不变...
有没有办法检查警告?
是我做错了什么,还是有什么可以检查的?
任何帮助将不胜感激......
本
【问题讨论】:
-
这看起来很奇怪,你能检查一下你得到了奇怪的结果吗:
print("initial params\n") print(gp.kernel.get_params()) gp.fit(X, y) print("\nlearned kernel params") print(gp.kernel_.get_params())抱歉格式很糟糕,但我还没有答案! -
get_params()只返回分类的超参数,而不是学习到的属性。超参数没有改变。请参阅attributes部分下的 the documentation 以了解将要更改的内容。 -
@Vivek Kumar
kernel_(注意下划线)属性确实包含标准示例的优化超参数 -
但是你又在内核上调用 get_params() ,它再次只会返回初始化时使用的参数。内核对象的优化参数是
theta和bounds。见this for example -
@Vivek Kumar 我打电话给
gp.kernel_.get_params():)
标签: python scikit-learn regression gaussian