50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > 【Python实例第36讲】一个高斯过程回归的例子

【Python实例第36讲】一个高斯过程回归的例子

时间:2023-01-20 05:18:47

相关推荐

【Python实例第36讲】一个高斯过程回归的例子

机器学习训练营——机器学习爱好者的自由交流空间(入群联系qq:2279055353)

这是一个简单的一维回归的例子,以两种不同的方式计算:

一个是无噪音的情况

另一个是噪音已知的情况

在这两种情况下的核参数都由最大似然法估计得到。

实例代码

import numpy as npfrom matplotlib import pyplot as pltfrom sklearn.gaussian_process import GaussianProcessRegressorfrom sklearn.gaussian_process.kernels import RBF, ConstantKernel as Cnp.random.seed(1)def f(x):"""The function to predict."""return x * np.sin(x)# ----------------------------------------------------------------------# First the noiseless caseX = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T# Observationsy = f(X).ravel()# Mesh the input space for evaluations of the real function, the prediction and# its MSEx = np.atleast_2d(np.linspace(0, 10, 1000)).T# Instantiate a Gaussian Process modelkernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)# Fit to data using Maximum Likelihood Estimation of the parametersgp.fit(X, y)# Make the prediction on the meshed x-axis (ask for MSE as well)y_pred, sigma = gp.predict(x, return_std=True)# Plot the function, the prediction and the 95% confidence interval based on# the MSEplt.figure()plt.plot(x, f(x), 'r:', label=u'$f(x) = x\,\sin(x)$')plt.plot(X, y, 'r.', markersize=10, label=u'Observations')plt.plot(x, y_pred, 'b-', label=u'Prediction')plt.fill(np.concatenate([x, x[::-1]]),np.concatenate([y_pred - 1.9600 * sigma,(y_pred + 1.9600 * sigma)[::-1]]),alpha=.5, fc='b', ec='None', label='95% confidence interval')plt.xlabel('$x$')plt.ylabel('$f(x)$')plt.ylim(-10, 20)plt.legend(loc='upper left')# ----------------------------------------------------------------------# now the noisy caseX = np.linspace(0.1, 9.9, 20)X = np.atleast_2d(X).T# Observations and noisey = f(X).ravel()dy = 0.5 + 1.0 * np.random.random(y.shape)noise = np.random.normal(0, dy)y += noise# Instantiate a Gaussian Process modelgp = GaussianProcessRegressor(kernel=kernel, alpha=dy ** 2,n_restarts_optimizer=10)# Fit to data using Maximum Likelihood Estimation of the parametersgp.fit(X, y)# Make the prediction on the meshed x-axis (ask for MSE as well)y_pred, sigma = gp.predict(x, return_std=True)# Plot the function, the prediction and the 95% confidence interval based on# the MSEplt.figure()plt.plot(x, f(x), 'r:', label=u'$f(x) = x\,\sin(x)$')plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label=u'Observations')plt.plot(x, y_pred, 'b-', label=u'Prediction')plt.fill(np.concatenate([x, x[::-1]]),np.concatenate([y_pred - 1.9600 * sigma,(y_pred + 1.9600 * sigma)[::-1]]),alpha=.5, fc='b', ec='None', label='95% confidence interval')plt.xlabel('$x$')plt.ylabel('$f(x)$')plt.ylim(-10, 20)plt.legend(loc='upper left')plt.show()

精彩内容,请关注微信公众号:统计学习与大数据

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。