Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 26 additions & 43 deletions examples/gaussian_process/plot_gp_diabetes_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,52 @@
# -*- coding: utf-8 -*-

"""
=========================================================================
========================================================================
Gaussian Processes regression: goodness-of-fit on the 'diabetes' dataset
=========================================================================
========================================================================

This example consists in fitting a Gaussian Process model onto the diabetes
dataset.
WARNING: This is quite time consuming (about 2 minutes overall runtime).

The corelation parameters are given in order to maximize the generalization
capacity of the model. We assumed an anisotropic squared exponential
correlation model with a constant regression model. We also used a
nugget = 1e-2 in order to account for the (strong) noise in the targets.
The correlation parameters are determined by means of maximum likelihood
estimation (MLE). An anisotropic squared exponential correlation model with a
constant regression model are assumed. We also used a nugget = 1e-2 in order to
account for the (strong) noise in the targets.

The figure is a goodness-of-fit plot obtained using leave-one-out predictions
of the Gaussian Process model. Based on these predictions, we compute an
explained variance error (Q2).
We compute then compute a cross-validation estimate of the coefficient of
determination (R2) without reperforming MLE, using the set of correlation
parameters found on the whole dataset.
"""
print __doc__

# Author: Vincent Dubourg <[email protected]>
# License: BSD style

from scikits.learn import datasets, cross_val, metrics
import numpy as np
from scikits.learn import datasets
from scikits.learn.gaussian_process import GaussianProcess
from matplotlib import pyplot as pl

# Print the docstring
print __doc__
from scikits.learn.cross_val import cross_val_score, KFold
from scikits.learn.metrics import r2_score

# Load the dataset from scikits' data sets
diabetes = datasets.load_diabetes()
X, y = diabetes['data'], diabetes['target']
X, y = diabetes.data, diabetes.target

# Instanciate a GP model
gp = GaussianProcess(regr='constant', corr='absolute_exponential',
theta0=[1e-4] * 10, thetaL=[1e-12] * 10,
thetaU=[1e-2] * 10, nugget=1e-2, optimizer='Welch',
verbose=False)
thetaU=[1e-2] * 10, nugget=1e-2, optimizer='Welch')

# Fit the GP model to the data
# Fit the GP model to the data performing maximum likelihood estimation
gp.fit(X, y)
gp.theta0 = gp.theta
gp.thetaL = None
gp.thetaU = None
gp.verbose = False

# Estimate the leave-one-out predictions using the cross_val module
n_jobs = 2 # the distributing capacity available on the machine
y_pred = y + cross_val.cross_val_score(gp, X, y=y,
cv=cross_val.LeaveOneOut(y.size),
n_jobs=n_jobs,
).ravel()

# Compute the empirical explained variance
Q2 = metrics.explained_variance_score(y, y_pred)
# Deactivate maximum likelihood estimation for the cross-validation loop
gp.theta0 = gp.theta # Given correlation parameter = MLE
gp.thetaL, gp.thetaU = None, None # None bounds deactivate MLE

# Goodness-of-fit plot
pl.figure()
pl.title('Goodness-of-fit plot (Q2 = %1.2e)' % Q2)
pl.plot(y, y_pred, 'r.', label='Leave-one-out')
pl.plot(y, gp.predict(X), 'k.', label='Whole dataset (nugget=1e-2)')
pl.plot([y.min(), y.max()], [y.min(), y.max()], 'k--')
pl.xlabel('Observations')
pl.ylabel('Predictions')
pl.legend(loc='upper left')
pl.axis('tight')
pl.show()
# Perform a cross-validation estimate of the coefficient of determination using
# the cross_val module using all CPUs available on the machine
K = 20 # folds
R2 = cross_val_score(gp, X, y=y, cv=KFold(y.size, K), n_jobs=-1).mean()
print("The %d-Folds estimate of the coefficient of determination is R2 = %s"
% (K, R2))
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
corresponds to the 95% confidence interval on the prediction of the zero level
set.
"""
print __doc__

# Author: Vincent Dubourg <[email protected]>
# License: BSD style
Expand All @@ -24,22 +25,19 @@
from matplotlib import pyplot as pl
from matplotlib import cm

# Print the docstring
print __doc__

# Standard normal distribution functions
Grv = stats.distributions.norm()
phi = lambda x: Grv.pdf(x)
PHI = lambda x: Grv.cdf(x)
PHIinv = lambda x: Grv.ppf(x)
phi = stats.distributions.norm().pdf
PHI = stats.distributions.norm().cdf
PHIinv = stats.distributions.norm().ppf

# A few constants
lim = 8
b, kappa, e = 5, .5, .1

# The function to predict (classification will then consist in predicting
# wheter g(x) <= 0 or not)
g = lambda x: b - x[:, 1] - kappa * (x[:, 0] - e) ** 2.

def g(x):
"""The function to predict (classification will then consist in predicting
whether g(x) <= 0 or not)"""
return 5. - x[:, 1] - .5 * x[:, 0] ** 2.

# Design of experiments
X = np.array([[-4.61611719, -6.00099547],
Expand Down
12 changes: 6 additions & 6 deletions examples/gaussian_process/plot_gp_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-

"""
=================================================================
=========================================================
Gaussian Processes regression: basic introductory example
=================================================================
=========================================================

A simple one-dimensional regression exercise with a cubic correlation
model whose parameters are estimated using the maximum likelihood principle.
Expand All @@ -13,6 +13,7 @@
model as well as its probabilistic nature in the form of a pointwise 95%
confidence interval.
"""
print __doc__

# Author: Vincent Dubourg <[email protected]>
# License: BSD style
Expand All @@ -21,11 +22,10 @@
from scikits.learn.gaussian_process import GaussianProcess
from matplotlib import pyplot as pl

# Print the docstring
print __doc__

# The function to predict
f = lambda x: x * np.sin(x)
def f(x):
"""The function to predict."""
return x * np.sin(x)

# The design of experiments
X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
Expand Down
54 changes: 24 additions & 30 deletions scikits/learn/gaussian_process/correlation_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,20 @@ def absolute_exponential(theta, d):
An array with shape (n_eval, ) containing the values of the
autocorrelation model.
"""

theta = np.asanyarray(theta, dtype=np.float)
d = np.asanyarray(d, dtype=np.float)
d = np.abs(np.asanyarray(d, dtype=np.float))

if d.ndim > 1:
n_features = d.shape[1]
else:
n_features = 1

if theta.size == 1:
theta = np.repeat(theta, n_features)
return np.exp(- theta[0] * np.sum(d, axis=1))
elif theta.size != n_features:
raise ValueError("Length of theta must be 1 or " + str(n_features))

td = - theta.reshape(1, n_features) * abs(d)
r = np.exp(np.sum(td, 1))

return r
raise ValueError("Length of theta must be 1 or %s" % n_features)
else:
return np.exp(- np.sum(theta.reshape(1, n_features) * d, axis=1))


def squared_exponential(theta, d):
Expand Down Expand Up @@ -92,15 +89,13 @@ def squared_exponential(theta, d):
n_features = d.shape[1]
else:
n_features = 1

if theta.size == 1:
theta = np.repeat(theta, n_features)
return np.exp(- theta[0] * np.sum(d**2, axis=1))
elif theta.size != n_features:
raise Exception("Length of theta must be 1 or " + str(n_features))

td = - theta.reshape(1, n_features) * d ** 2
r = np.exp(np.sum(td, 1))

return r
raise ValueError("Length of theta must be 1 or %s" % n_features)
else:
return np.exp(- np.sum(theta.reshape(1, n_features) * d**2, axis=1))


def generalized_exponential(theta, d):
Expand Down Expand Up @@ -138,16 +133,17 @@ def generalized_exponential(theta, d):
n_features = d.shape[1]
else:
n_features = 1

lth = theta.size
if n_features > 1 and lth == 2:
theta = np.hstack([np.repeat(theta[0], n_features), theta[1]])
elif lth != n_features + 1:
raise Exception("Length of theta must be 2 or " + str(n_features + 1))
raise Exception("Length of theta must be 2 or %s" % (n_features + 1))
else:
theta = theta.reshape(1, lth)

td = - theta[:, 0:-1].reshape(1, n_features) * abs(d) ** theta[:, -1]
r = np.exp(np.sum(td, 1))
td = theta[:, 0:-1].reshape(1, n_features) * np.abs(d) ** theta[:, -1]
r = np.exp(- np.sum(td, 1))

return r

Expand Down Expand Up @@ -184,9 +180,7 @@ def pure_nugget(theta, d):

n_eval = d.shape[0]
r = np.zeros(n_eval)
# The ones on the diagonal of the correlation matrix are enforced within
# the KrigingModel instanciation to allow multiple design sites in this
# ordinary least squares context.
r[np.all(d == 0., axis=1)] = 1.

return r

Expand Down Expand Up @@ -225,15 +219,15 @@ def cubic(theta, d):
n_features = d.shape[1]
else:
n_features = 1

lth = theta.size
if lth == 1:
theta = np.repeat(theta, n_features)[np.newaxis][:]
td = np.abs(d) * theta
elif lth != n_features:
raise Exception("Length of theta must be 1 or " + str(n_features))
else:
theta = theta.reshape(1, n_features)
td = np.abs(d) * theta.reshape(1, n_features)

td = abs(d) * theta
td[td > 1.] = 1.
ss = 1. - td ** 2. * (3. - 2. * td)
r = np.prod(ss, 1)
Expand Down Expand Up @@ -275,15 +269,15 @@ def linear(theta, d):
n_features = d.shape[1]
else:
n_features = 1

lth = theta.size
if lth == 1:
theta = np.repeat(theta, n_features)[np.newaxis][:]
if lth == 1:
td = np.abs(d) * theta
elif lth != n_features:
raise Exception("Length of theta must be 1 or " + str(n_features))
raise Exception("Length of theta must be 1 or %s" % n_features)
else:
theta = theta.reshape(1, n_features)
td = np.abs(d) * theta.reshape(1, n_features)

td = abs(d) * theta
td[td > 1.] = 1.
ss = 1. - td
r = np.prod(ss, 1)
Expand Down
Loading