2010-11-14 18:55:52 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
2010-12-09 15:44:36 +08:00
|
|
|
=========================================================
|
2010-11-25 05:48:36 +08:00
|
|
|
Gaussian Processes regression: basic introductory example
|
2010-12-09 15:44:36 +08:00
|
|
|
=========================================================
|
2010-11-14 18:55:52 +08:00
|
|
|
|
2010-11-15 01:44:34 +08:00
|
|
|
A simple one-dimensional regression exercise with a cubic correlation
|
|
|
|
|
model whose parameters are estimated using the maximum likelihood principle.
|
|
|
|
|
|
|
|
|
|
The figure illustrates the interpolating property of the Gaussian Process
|
|
|
|
|
model as well as its probabilistic nature in the form of a pointwise 95%
|
|
|
|
|
confidence interval.
|
2010-11-14 18:55:52 +08:00
|
|
|
"""
|
2010-11-29 11:31:22 +08:00
|
|
|
print __doc__
|
2010-11-15 01:44:34 +08:00
|
|
|
|
2010-11-17 05:57:54 +08:00
|
|
|
# Author: Vincent Dubourg <vincent.dubourg@gmail.com>
|
2010-11-14 18:55:52 +08:00
|
|
|
# License: BSD style
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.gaussian_process import GaussianProcess
|
2010-11-14 18:55:52 +08:00
|
|
|
from matplotlib import pyplot as pl
|
|
|
|
|
|
2010-11-15 01:44:34 +08:00
|
|
|
|
2010-12-09 15:44:36 +08:00
|
|
|
def f(x):
|
2010-12-13 06:40:13 +08:00
|
|
|
"""The function to predict."""
|
2010-12-09 15:44:36 +08:00
|
|
|
return x * np.sin(x)
|
2010-11-14 18:55:52 +08:00
|
|
|
|
|
|
|
|
# The design of experiments
|
2010-11-19 15:26:17 +08:00
|
|
|
X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
|
2010-11-14 18:55:52 +08:00
|
|
|
|
|
|
|
|
# Observations
|
2010-11-24 06:22:12 +08:00
|
|
|
y = f(X).ravel()
|
2010-11-14 18:55:52 +08:00
|
|
|
|
2010-11-15 01:44:34 +08:00
|
|
|
# Mesh the input space for evaluations of the real function, the prediction and
|
|
|
|
|
# its MSE
|
2010-11-19 15:26:17 +08:00
|
|
|
x = np.atleast_2d(np.linspace(0, 10, 1000)).T
|
2010-11-14 18:55:52 +08:00
|
|
|
|
|
|
|
|
# Instanciate a Gaussian Process model
|
2010-11-21 22:42:37 +08:00
|
|
|
gp = GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1e-1, \
|
2010-11-15 01:44:34 +08:00
|
|
|
random_start=100)
|
2010-11-14 18:55:52 +08:00
|
|
|
|
|
|
|
|
# Fit to data using Maximum Likelihood Estimation of the parameters
|
2010-11-24 06:22:12 +08:00
|
|
|
gp.fit(X, y)
|
2010-11-14 18:55:52 +08:00
|
|
|
|
|
|
|
|
# Make the prediction on the meshed x-axis (ask for MSE as well)
|
2010-11-24 06:22:12 +08:00
|
|
|
y_pred, MSE = gp.predict(x, eval_MSE=True)
|
2010-11-14 18:55:52 +08:00
|
|
|
sigma = np.sqrt(MSE)
|
|
|
|
|
|
2010-11-15 01:44:34 +08:00
|
|
|
# Plot the function, the prediction and the 95% confidence interval based on
|
|
|
|
|
# the MSE
|
2010-11-14 18:55:52 +08:00
|
|
|
fig = pl.figure()
|
|
|
|
|
pl.plot(x, f(x), 'r:', label=u'$f(x) = x\,\sin(x)$')
|
2010-11-24 06:22:12 +08:00
|
|
|
pl.plot(X, y, 'r.', markersize=10, label=u'Observations')
|
|
|
|
|
pl.plot(x, y_pred, 'b-', label=u'Prediction')
|
2010-11-15 01:44:34 +08:00
|
|
|
pl.fill(np.concatenate([x, x[::-1]]), \
|
2010-11-24 06:22:12 +08:00
|
|
|
np.concatenate([y_pred - 1.9600 * sigma,
|
|
|
|
|
(y_pred + 1.9600 * sigma)[::-1]]), \
|
2010-11-15 01:44:34 +08:00
|
|
|
alpha=.5, fc='b', ec='None', label='95% confidence interval')
|
2010-11-14 18:55:52 +08:00
|
|
|
pl.xlabel('$x$')
|
|
|
|
|
pl.ylabel('$f(x)$')
|
|
|
|
|
pl.ylim(-10, 20)
|
|
|
|
|
pl.legend(loc='upper left')
|
|
|
|
|
|
|
|
|
|
pl.show()
|