优化Scikit中自定义高斯过程核的网格搜索

我在使用高斯过程时,遇到使用scikit-learn的GP模块创建和优化自定义核的问题,特别是在使用gridsearchcv时。最能描述这个问题的例子是使用经典的Mauna Loa例子,在这个例子中,合适的核是通过组合已定义的核如RBFRationalQuadratic来构建的。在这个例子中,自定义核的参数没有被优化,而是被视为给定的。如果我想运行一个更一般的案例,通过交叉验证来估计这些超参数,该怎么做呢?我应该如何构建自定义核,然后构建相应的param_grid对象进行网格搜索呢?

以一种非常简单的方式,我可以像这样构建一个自定义核:

def custom_kernel(a,ls,l,alpha,nl):    kernel = a*RBF(length_scale=ls) \    + b*RationalQuadratic(length_scale=l,alpha=alpha) \    + WhiteKernel(noise_level=nl)    return kernel

然而,这个函数当然不能通过gridsearchcv调用,例如GaussianProcessRegressor(kernel=custom_kernel(a,ls,l,alpha,nl))

一种可能的解决方案在这个StackOverflow问题中被提出,但是我想知道是否有更简单的方法解决这个问题,而不是从头开始编写核(连同其超参数),因为我希望使用标准核的组合,并且还有可能想要混合使用它们。


回答:

所以这就是我目前的进展。这回答了问题,但对于Mauna Loa的例子来说,速度非常慢,不过这可能是一个难处理的数据集:

from sklearn.gaussian_process import GaussianProcessRegressorfrom sklearn.model_selection import GridSearchCVfrom sklearn.gaussian_process.kernels import ConstantKernel,RBF,WhiteKernel,RationalQuadratic,ExpSineSquaredimport numpy as npfrom sklearn.datasets import fetch_openml# from https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpr_co2.htmldef load_mauna_loa_atmospheric_co2():    ml_data = fetch_openml(data_id=41187)    months = []    ppmv_sums = []    counts = []    y = ml_data.data[:, 0]    m = ml_data.data[:, 1]    month_float = y + (m - 1) / 12    ppmvs = ml_data.target    for month, ppmv in zip(month_float, ppmvs):        if not months or month != months[-1]:            months.append(month)            ppmv_sums.append(ppmv)            counts.append(1)        else:            # aggregate monthly sum to produce average            ppmv_sums[-1] += ppmv            counts[-1] += 1    months = np.asarray(months).reshape(-1, 1)    avg_ppmvs = np.asarray(ppmv_sums) / counts    return months, avg_ppmvsX, y = load_mauna_loa_atmospheric_co2()# Kernel with parameters given in GPML bookk1 = ConstantKernel(constant_value=66.0**2) * RBF(length_scale=67.0)  # long term smooth rising trendk2 = ConstantKernel(constant_value=2.4**2) * RBF(length_scale=90.0) \    * ExpSineSquared(length_scale=1.3, periodicity=1.0)  # seasonal component# medium term irregularityk3 = ConstantKernel(constant_value=0.66**2) \    * RationalQuadratic(length_scale=1.2, alpha=0.78)k4 = ConstantKernel(constant_value=0.18**2) * RBF(length_scale=0.134) \    + WhiteKernel(noise_level=0.19**2)  # noise termskernel_gpml = k1 + k2 + k3 + k4gp = GaussianProcessRegressor(kernel=kernel_gpml)# print parametersprint(gp.get_params())param_grid = {'alpha': np.logspace(-2, 4, 5),              'kernel__k1__k1__k1__k1__constant_value': np.logspace(-2, 4, 5),              'kernel__k1__k1__k1__k2__length_scale': np.logspace(-2, 2, 5),              'kernel__k2__k2__noise_level':np.logspace(-2, 1, 5)              }grid_gp = GridSearchCV(gp,cv=5,param_grid=param_grid,n_jobs=4)grid_gp.fit(X, y)

对我有帮助的是首先将模型初始化为gp = GaussianProcessRegressor(kernel=kernel_gpml),然后使用get_params属性来获取模型超参数的列表。

最后,我注意到在他们的中,Rasmussen和Williams似乎使用了留一法交叉验证来调整超参数。

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注