GaussianProcessRegressor
- class sklearn.gaussian_process.GaussianProcessRegressor(kernel=None, *, alpha=1e-10, optimizer='fmin_l_bfgs_b', n_restarts_optimizer=0, normalize_y=False, copy_X_train, ,
Gaussian process regression (GPR).
The implementation is based on Algorithm 2.1 of [RW2006].
In addition to standard scikit-learn estimator API,
GaussianProcessRegressor:
allows prediction without prior fitting (based on the GP prior)
provides an additional method sample_y(X), which evaluates samples
drawn from the GPR (prior or posterior) at given inputs
exposes a method log_marginal_likelihood(theta), which can be used
externally for other ways of selecting hyperparameters, e.g., via
Markov chain Monte Carlo.
To learn the difference between a point-estimate approach vs. a more Bayesian modelling approach, refer to the example entitled Comparison of kernel ridge and Gaussian process regression.
Read more in the User Guide.
The kernel specifying the covariance function of the GP.
If None is passed,
the kernel ConstantKernel() * RBF() is used as default.
Note that
the kernel hyperparameters are optimized during fitting
unless the bounds are marked as "fixed"
or the argument optimizer is set to None.
Value added to the diagonal of the kernel matrix during fitting. This can prevent a potential numerical issue during fitting, by ensuring that the calculated values form a positive definite matrix. It can also be interpreted as the variance of additional Gaussian measurement noise on the training observations. Note that this is different from using a . If an array is passed, it must have the same number of entries as the data used for fitting and is used as datapoint-dependent noise level. Allowing to specify the noise level directly as a parameter is mainly for convenience and for consistency with . For an example illustrating how the alpha parameter controls the noise variance in Gaussian Process Regression, see .
>>> from sklearn.datasets import make_friedman2
>>> from sklearn.gaussian_process import GaussianProcessRegressor
>>> from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel
>>> X, y = make_friedman2(n_samples=500, noise=0
Fit Gaussian process regression model.
Feature vectors or other representations of training data.
Target values.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Return log-marginal likelihood of theta for training data.
Kernel hyperparameters for which the log-marginal likelihood is
evaluated. If None, the precomputed log_marginal_likelihood
of self.kernel_.theta is returned.
Predict using the Gaussian process regression model.
We can also predict based on an unfitted model by using the GP prior.
In addition to the mean of the predictive distribution, optionally also
returns its standard deviation (return_std=True) or covariance
(return_cov=True). Note that at most one of the two can be requested.
Draw samples from Gaussian process and evaluate at X.
Query points where the GP is evaluated.
Number of samples drawn from the Gaussian process per query point.
Return coefficient of determination on test data.
The coefficient of determination, , is defined as , where is the residual sum of squares and is the total sum of squares . The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of , disregarding the input features, would get a score of 0.0.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Configure whether metadata should be requested to be passed to the method.
Comparison of kernel ridge and Gaussian process regression
Forecasting of CO2 level on Mona Loa dataset using Gaussian process regression (GPR)
Ability of Gaussian process regression (GPR) to estimate data noise-level
Gaussian Processes regression: basic introductory example
Illustration of prior and posterior Gaussian process for different kernels
WhiteKernelCan either be one of the internally supported optimizers for optimizing the kernel’s parameters, specified by a string, or an externally defined optimizer passed as a callable. If a callable is passed, it must have the signature:
def optimizer(obj_func, initial_theta, bounds):
# * 'obj_func': the objective function to be minimized, which
# takes the hyperparameters theta as a parameter and an
# optional flag eval_gradient, which determines if the
# gradient is returned additionally to the function value
# * 'initial_theta': the initial value for theta, which can be
# used by local optimizers
# * 'bounds': the bounds on the values of theta
....
# Returned are the best found hyperparameters theta and
# the corresponding value of the target function.
return theta_opt, func_min
Per default, the L-BFGS-B algorithm from scipy.optimize.minimize
is used. If None is passed, the kernel’s parameters are kept fixed.
Available internal optimizers are: {'fmin_l_bfgs_b'}.
The number of restarts of the optimizer for finding the kernel’s
parameters which maximize the log-marginal likelihood. The first run
of the optimizer is performed from the kernel’s initial parameters,
the remaining ones (if any) from thetas sampled log-uniform randomly
from the space of allowed theta-values. If greater than 0, all bounds
must be finite. Note that n_restarts_optimizer == 0 implies that one
run is performed.
Whether or not to normalize the target values y by removing the mean
and scaling to unit-variance. This is recommended for cases where
zero-mean, unit-variance priors are used. Note that, in this
implementation, the normalisation is reversed before the GP predictions
are reported.
If True, a persistent copy of the training data is stored in the object. Otherwise, just a reference to the training data is stored, which might cause predictions to change if the data is modified externally.
The number of dimensions of the target values. Used to decide the number
of outputs when sampling from the prior distributions (i.e. calling
sample_y before fit). This parameter is ignored once
fit has been called.
Determines random number generation used to initialize the centers. Pass an int for reproducible results across multiple function calls. See Glossary.
Feature vectors or other representations of training data (also required for prediction).
Target values in training data (also required for prediction).
The kernel used for prediction. The structure of the kernel is the same as the one passed as parameter but with optimized hyperparameters.
Lower-triangular Cholesky decomposition of the kernel in X_train_.
Dual coefficients of training data points in kernel space.
The log-marginal-likelihood of self.kernel_.theta.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
GaussianProcessRegressor class instance.
If True, the gradient of the log-marginal likelihood with respect to the kernel hyperparameters at position theta is returned additionally. If True, theta must not be None.
If True, the kernel attribute is copied. If False, the kernel attribute is modified, but may result in a performance improvement.
Log-marginal likelihood of theta for training data.
Gradient of the log-marginal likelihood with respect to the kernel hyperparameters at position theta. Only returned when eval_gradient is True.
Query points where the GP is evaluated.
If True, the standard-deviation of the predictive distribution at the query points is returned along with the mean.
If True, the covariance of the joint predictive distribution at the query points is returned along with the mean.
Mean of predictive distribution at query points.
Standard deviation of predictive distribution at query points.
Only returned when return_std is True.
Covariance of joint predictive distribution at query points.
Only returned when return_cov is True.
Determines random number generation to randomly draw samples. Pass an int for reproducible results across multiple function calls. See Glossary.
Values of n_samples samples drawn from Gaussian process and evaluated at query points.
((y_true - y_pred)** 2).sum()((y_true - y_true.mean()) ** 2).sum()yTest samples. For some estimators this may be a precomputed
kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted), where n_samples_fitted
is the number of samples used in the fitting for the estimator.
True values for X.
Sample weights.
of self.predict(X) w.r.t. y.
The score used when calling score on a regressor uses
multioutput='uniform_average' from version 0.23 to keep consistent
with default value of r2_score.
This influences the score method of all the multioutput
regressors (except for
MultiOutputRegressor).
Estimator instance.
Configure whether metadata should be requested to be passed to the predict method.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to predict.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for return_cov parameter in predict.
Metadata routing for return_std parameter in predict.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to score.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for sample_weight parameter in score.
The updated object.
The updated object.