如何在sci-kit learn中使用GP.fit处理多维输入?

能否提供一个示例?我正在尝试将其用于5D输入。另外,我如何为每个输入绘制与输出的图表?我只有一个输出维度。我的想法是传递一些训练集数据,然后使用测试数据集验证输出。我想传递一个5D(X1 X2 X3 X4 X5)的输入,其中我有1600个数据点。目前我只有X1作为输入

这是代码:

from matplotlib import pyplot as pltfrom sklearn.gaussian_process import GaussianProcessRegressorfrom sklearn.base import BaseEstimatorfrom sklearn.gaussian_process.kernels import RBF, Matern, WhiteKernel, ConstantKernel, RationalQuadratic, ExpSineSquared, DotProduct# define Kernelimport numpy as npkernels = [1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0)),           1.0 * Matern(length_scale=1.0, length_scale_bounds=(1e-1, 10.0),                        nu=1.5),           1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1),           1.0 * ExpSineSquared(length_scale=1.0, periodicity=3.0,                                length_scale_bounds=(0.1, 10.0),                                periodicity_bounds=(1.0, 10.0)),           ConstantKernel(0.1, (0.01, 10.0))               * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.0, 10.0)) ** 2),           ]# Define inputs and outputsx = np.array([-5.2,-3,-2,-1,1,5], ndmin=2).TX = x.reshape(-1, 1)y =np.array([-2,0,1,2,-1,1])max_x = max(x)min_x = min (x)max_y = max (y)min_y = min(y)for fig_index, kernel in enumerate(kernels):    # call GP regression library and fit inputs to output    gp = gaussian_process.GaussianProcessRegressor(kernel=kernel)    gp.fit(X, y)#     parameter = get_params(deep=True)#     print(parameter)               gp.kernel_    print(gp.kernel_)    plt.figure(fig_index, figsize=(10,6))    plt.subplot(2,1,1)    x_pred = np.array(np.linspace(-5, 5,50), ndmin=2).T    # Mark the observations    plt.plot(X, y, 'ro', label='observations')    X_test = np.array(np.linspace(max_x+1, min_x-1, 1000),ndmin=2).T    y_mean, y_std = gp.predict(X_test, return_std=True)    # Draw a mean function and 95% confidence interval    plt.plot(X_test, y_mean, 'b-', label='mean function')    upper_bound = y_mean +y_std    lower_bound = y_mean - y_std    plt.fill_between(X_test.ravel(), lower_bound, upper_bound, color = 'k', alpha = 0.2,                 label='95% confidence interval')    # plot posterior    y_sample = gp.sample_y(X_test,4)    plt.plot(X_test,y_sample,lw=1)    plt.scatter(X[:,0],y,c='r',s=50,zorder=10,edgecolor=(0,0,0))    plt.title("Posterior (kernel:%s)\n Log-Likelihood: %3f"             % (gp.kernel_, gp.log_marginal_likelihood(gp.kernel_.theta)),              fontsize=14)    plt.tight_layout()    plt.show()

回答:

在使用多输入进行GP回归时,除了在各向异性情况下需要在核函数定义中明确提供相关参数外,没有什么特别之处。

这里是一个简单示例,使用与您类似的虚拟5D数据和各向同性的RBF核函数:

from sklearn.gaussian_process import GaussianProcessRegressorfrom sklearn.gaussian_process.kernels import RBFfrom sklearn.datasets import make_regressionimport numpy as np# dummy data:X, y = make_regression(n_samples=20, n_features=5, n_targets=1)X.shape# (20, 5)kernel = RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))gp = GaussianProcessRegressor(kernel=kernel)gp.fit(X, y)# GaussianProcessRegressor(alpha=1e-10, copy_X_train=True,#             kernel=RBF((length_scale=1), n_restarts_optimizer=0,#             normalize_y=False, optimizer='fmin_l_bfgs_b',#             random_state=None)

更新:在各向异性情况下,您应该在核函数中明确定义不同的参数;这里是一个RBF核函数和一个2D变量的示例定义:

kernel = RBF(length_scale=[1.0, 2.0], length_scale_bounds=[(1e-1, 10.0), (1e-2, 1.0)])

对于5D情况,相应地进行扩展。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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