优化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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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