package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `GaussianProcessRegressor
]
type t = [ `BaseEstimator | `GaussianProcessRegressor | `MultiOutputMixin | `Object | `RegressorMixin ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val as_regressor : t -> [ `RegressorMixin ] Obj.t
val as_multi_output : t -> [ `MultiOutputMixin ] Obj.t
val as_estimator : t -> [ `BaseEstimator ] Obj.t
val create : ?kernel:Py.Object.t -> ?alpha:[> `ArrayLike ] Np.Obj.t -> ?optimizer:[ `Callable of Py.Object.t | `S of string ] -> ?n_restarts_optimizer:int -> ?normalize_y:bool -> ?copy_X_train:bool -> ?random_state:int -> unit -> t

Gaussian process regression (GPR).

The implementation is based on Algorithm 2.1 of Gaussian Processes for Machine Learning (GPML) by Rasmussen and Williams.

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.

Read more in the :ref:`User Guide <gaussian_process>`.

.. versionadded:: 0.18

Parameters ---------- kernel : kernel object The kernel specifying the covariance function of the GP. If None is passed, the kernel '1.0 * RBF(1.0)' is used as default. Note that the kernel's hyperparameters are optimized during fitting.

alpha : float or array-like, optional (default: 1e-10) Value added to the diagonal of the kernel matrix during fitting. Larger values correspond to increased noise level in the observations. This can also prevent a potential numerical issue during fitting, by ensuring that the calculated values form a positive definite matrix. 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. Note that this is equivalent to adding a WhiteKernel with c=alpha. Allowing to specify the noise level directly as a parameter is mainly for convenience and for consistency with Ridge.

optimizer : string or callable, optional (default: 'fmin_l_bfgs_b') Can 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' is the objective function to be minimized, which # takes the hyperparameters theta as 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-BGFS-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'

n_restarts_optimizer : int, optional (default: 0) 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.

normalize_y : boolean, optional (default: False) Whether the target values y are normalized, i.e., the mean of the observed target values become zero. This parameter should be set to True if the target values' mean is expected to differ considerable from zero. When enabled, the normalization effectively modifies the GP's prior based on the data, which contradicts the likelihood principle; normalization is thus disabled per default.

copy_X_train : bool, optional (default: True) 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.

random_state : int, RandomState instance or None, optional (default: None) The generator used to initialize the centers. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`.

Attributes ---------- X_train_ : sequence of length n_samples Feature vectors or other representations of training data (also required for prediction). Could either be array-like with shape = (n_samples, n_features) or a list of objects.

y_train_ : array-like of shape (n_samples,) or (n_samples, n_targets) Target values in training data (also required for prediction)

kernel_ : kernel object The kernel used for prediction. The structure of the kernel is the same as the one passed as parameter but with optimized hyperparameters

L_ : array-like of shape (n_samples, n_samples) Lower-triangular Cholesky decomposition of the kernel in ``X_train_``

alpha_ : array-like of shape (n_samples,) Dual coefficients of training data points in kernel space

log_marginal_likelihood_value_ : float The log-marginal-likelihood of ``self.kernel_.theta``

Examples -------- >>> 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, random_state=0) >>> kernel = DotProduct() + WhiteKernel() >>> gpr = GaussianProcessRegressor(kernel=kernel, ... random_state=0).fit(X, y) >>> gpr.score(X, y) 0.3680... >>> gpr.predict(X:2,:, return_std=True) (array(653.0..., 592.1...), array(316.6..., 316.6...))

val fit : x:Py.Object.t -> y:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> t

Fit Gaussian process regression model.

Parameters ---------- X : sequence of length n_samples Feature vectors or other representations of training data. Could either be array-like with shape = (n_samples, n_features) or a list of objects.

y : array-like of shape (n_samples,) or (n_samples, n_targets) Target values

Returns ------- self : returns an instance of self.

val get_params : ?deep:bool -> [> tag ] Obj.t -> Dict.t

Get parameters for this estimator.

Parameters ---------- deep : bool, default=True If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns ------- params : mapping of string to any Parameter names mapped to their values.

val log_marginal_likelihood : ?theta:[> `ArrayLike ] Np.Obj.t -> ?eval_gradient:bool -> ?clone_kernel:bool -> [> tag ] Obj.t -> float * [> `ArrayLike ] Np.Obj.t

Returns log-marginal likelihood of theta for training data.

Parameters ---------- theta : array-like of shape (n_kernel_params,) or None Kernel hyperparameters for which the log-marginal likelihood is evaluated. If None, the precomputed log_marginal_likelihood of ``self.kernel_.theta`` is returned.

eval_gradient : bool, default: False 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.

clone_kernel : bool, default=True If True, the kernel attribute is copied. If False, the kernel attribute is modified, but may result in a performance improvement.

Returns ------- log_likelihood : float Log-marginal likelihood of theta for training data.

log_likelihood_gradient : array, shape = (n_kernel_params,), optional Gradient of the log-marginal likelihood with respect to the kernel hyperparameters at position theta. Only returned when eval_gradient is True.

val predict : ?return_std:bool -> ?return_cov:bool -> x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

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, also its standard deviation (return_std=True) or covariance (return_cov=True). Note that at most one of the two can be requested.

Parameters ---------- X : sequence of length n_samples Query points where the GP is evaluated. Could either be array-like with shape = (n_samples, n_features) or a list of objects.

return_std : bool, default: False If True, the standard-deviation of the predictive distribution at the query points is returned along with the mean.

return_cov : bool, default: False If True, the covariance of the joint predictive distribution at the query points is returned along with the mean

Returns ------- y_mean : array, shape = (n_samples, n_output_dims) Mean of predictive distribution a query points

y_std : array, shape = (n_samples,), optional Standard deviation of predictive distribution at query points. Only returned when return_std is True.

y_cov : array, shape = (n_samples, n_samples), optional Covariance of joint predictive distribution a query points. Only returned when return_cov is True.

val sample_y : ?n_samples:int -> ?random_state:int -> x:Py.Object.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Draw samples from Gaussian process and evaluate at X.

Parameters ---------- X : sequence of length n_samples Query points where the GP is evaluated. Could either be array-like with shape = (n_samples, n_features) or a list of objects.

n_samples : int, default: 1 The number of samples drawn from the Gaussian process

random_state : int, RandomState instance or None, optional (default=0) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`.

Returns ------- y_samples : array, shape = (n_samples_X, n_output_dims, n_samples) Values of n_samples samples drawn from Gaussian process and evaluated at query points.

val score : ?sample_weight:[> `ArrayLike ] Np.Obj.t -> x:[> `ArrayLike ] Np.Obj.t -> y:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> float

Return the coefficient of determination R^2 of the prediction.

The coefficient R^2 is defined as (1 - u/v), where u is the residual sum of squares ((y_true - y_pred) ** 2).sum() and v is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). 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 y, disregarding the input features, would get a R^2 score of 0.0.

Parameters ---------- X : array-like of shape (n_samples, n_features) Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead, shape = (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

y : array-like of shape (n_samples,) or (n_samples, n_outputs) True values for X.

sample_weight : array-like of shape (n_samples,), default=None Sample weights.

Returns ------- score : float R^2 of self.predict(X) wrt. y.

Notes ----- The R2 score used when calling ``score`` on a regressor will use ``multioutput='uniform_average'`` from version 0.23 to keep consistent with :func:`~sklearn.metrics.r2_score`. This will influence the ``score`` method of all the multioutput regressors (except for :class:`~sklearn.multioutput.MultiOutputRegressor`). To specify the default value manually and avoid the warning, please either call :func:`~sklearn.metrics.r2_score` directly or make a custom scorer with :func:`~sklearn.metrics.make_scorer` (the built-in scorer ``'r2'`` uses ``multioutput='uniform_average'``).

val set_params : ?params:(string * Py.Object.t) list -> [> tag ] Obj.t -> t

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object.

Parameters ---------- **params : dict Estimator parameters.

Returns ------- self : object Estimator instance.

val x_train_ : t -> Py.Object.t

Attribute X_train_: get value or raise Not_found if None.

val x_train_opt : t -> Py.Object.t option

Attribute X_train_: get value as an option.

val y_train_ : t -> [> `ArrayLike ] Np.Obj.t

Attribute y_train_: get value or raise Not_found if None.

val y_train_opt : t -> [> `ArrayLike ] Np.Obj.t option

Attribute y_train_: get value as an option.

val kernel_ : t -> Py.Object.t

Attribute kernel_: get value or raise Not_found if None.

val kernel_opt : t -> Py.Object.t option

Attribute kernel_: get value as an option.

val l_ : t -> [> `ArrayLike ] Np.Obj.t

Attribute L_: get value or raise Not_found if None.

val l_opt : t -> [> `ArrayLike ] Np.Obj.t option

Attribute L_: get value as an option.

val alpha_ : t -> [> `ArrayLike ] Np.Obj.t

Attribute alpha_: get value or raise Not_found if None.

val alpha_opt : t -> [> `ArrayLike ] Np.Obj.t option

Attribute alpha_: get value as an option.

val log_marginal_likelihood_value_ : t -> float

Attribute log_marginal_likelihood_value_: get value or raise Not_found if None.

val log_marginal_likelihood_value_opt : t -> float option

Attribute log_marginal_likelihood_value_: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.