Toggle Menu
Prev Up Next

scikit-learn 1.3.2
Other versions

Please cite us if you use the software.

# `sklearn.gaussian_process`.GaussianProcessClassifier

class sklearn.gaussian_process.GaussianProcessClassifier(kernel=None, *, optimizer='fmin_l_bfgs_b', n_restarts_optimizer=0, max_iter_predict=100, warm_start=False, copy_X_train=True, random_state=None, multi_class='one_vs_rest', n_jobs=None)

  Gaussian process classification (GPC) based on Laplace approximation.

  The implementation is based on Algorithm 3.1, 3.2, and 5.1 from [RW2006].

  Internally, the Laplace approximation is used for approximating the non-Gaussian posterior by a Gaussian.

  Currently, the implementation is restricted to using the logistic link function. For multi-class classification, several binary one-versus rest classifiers are fitted. Note that this class thus does not implement a true multi-class Laplace approximation.

  Read more in the User Guide.

  New in version 0.18.

  Parameters:
    kernel kernel instance, default=None

      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. Also kernel cannot be a `CompoundKernel`.

    optimizer‘fmin_l_bfgs_b’, callable or None, 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 maximized, 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-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'

    n_restarts_optimizer int, 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.

    max_iter_predict int, default=100

      The maximum number of iterations in Newton’s method for approximating the posterior during predict. Smaller values will reduce computation time at the cost of worse results.

    warm_start bool, default=False

      If warm-starts are enabled, the solution of the last Newton iteration on the Laplace approximation of the posterior mode is used as initialization for the next call of _posterior_mode(). This can speed up convergence when _posterior_mode is called several times on similar problems as in hyperparameter optimization. See the Glossary.

    copy_X_train bool, 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, default=None

      Determines random number generation used to initialize the centers. Pass an int for reproducible results across multiple function calls. See Glossary.

    multi_class{‘one_vs_rest’, ‘one_vs_one’}, default=’one_vs_rest’

      Specifies how multi-class classification problems are handled. Supported are ‘one_vs_rest’ and ‘one_vs_one’. In ‘one_vs_rest’, one binary Gaussian process classifier is fitted for each class, which is trained to separate this class from the rest. In ‘one_vs_one’, one binary Gaussian process classifier is fitted for each pair of classes, which is trained to separate these two classes. The predictions of these binary predictors are combined into multi-class predictions. Note that ‘one_vs_one’ does not support predicting probability estimates.

    n_jobs int, default=None

      The number of jobs to use for the computation: the specified multiclass problems are computed in parallel. `None` means 1 unless in a `joblib.parallel_backend` context. `-1` means using all processors. See Glossary for more details.

  Attributes:
    base_estimator_`Estimator` instance

      The estimator instance that defines the likelihood function using the observed data.

    `kernel_`kernel instance

      Return the kernel of the base estimator.

    log_marginal_likelihood_value_float

      The log-marginal-likelihood of `self.kernel_.theta`

    classes_array-like of shape (n_classes,)

      Unique class labels.

    n_classes_int

      The number of classes in the training data

    n_features_in_int

      Number of features seen during fit.

      New in version 0.24.

    feature_names_in_ndarray of shape (`n_features_in_`,)

      Names of features seen during fit. Defined only when `X` has feature names that are all strings.

      New in version 1.0.

  See also

  `GaussianProcessRegressor`

    Gaussian process regression (GPR).

  References

  [RW2006]

  Carl E. Rasmussen and Christopher K.I. Williams, “Gaussian Processes for Machine Learning”, MIT Press 2006

  Examples

      >>> from sklearn.datasets import load_iris
      >>> from sklearn.gaussian_process import GaussianProcessClassifier
      >>> from sklearn.gaussian_process.kernels import RBF
      >>> X, y = load_iris(return_X_y=True)
      >>> kernel = 1.0 * RBF(1.0)
      >>> gpc = GaussianProcessClassifier(kernel=kernel,
      ...         random_state=0).fit(X, y)
      >>> gpc.score(X, y)
      0.9866...
      >>> gpc.predict_proba(X[:2,:])
      array([[0.83548752, 0.03228706, 0.13222543],
             [0.79064206, 0.06525643, 0.14410151]])

  Methods

  `fit`(X, y) | Fit Gaussian process classification model.
  `get_metadata_routing`() | Get metadata routing of this object.
  `get_params`([deep]) | Get parameters for this estimator.
  `log_marginal_likelihood`([theta, ...]) | Return log-marginal likelihood of theta for training data.
  `predict`(X) | Perform classification on an array of test vectors X.
  `predict_proba`(X) | Return probability estimates for the test vector X.
  `score`(X, y[, sample_weight]) | Return the mean accuracy on the given test data and labels.
  `set_params`(**params) | Set the parameters of this estimator.
  `set_score_request`(*[, sample_weight]) | Request metadata passed to the `score` method.

  fit(X, y)

    Fit Gaussian process classification model.

    Parameters:
      X array-like of shape (n_samples, n_features) or list of object

        Feature vectors or other representations of training data.

      y array-like of shape (n_samples,)

        Target values, must be binary.

    Returns:
      self object

        Returns an instance of self.

  get_metadata_routing()

    Get metadata routing of this object.

    Please check User Guide on how the routing mechanism works.

    Returns:
      routing MetadataRequest

        A `MetadataRequest` encapsulating routing information.

  get_params(deep=True)

    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 dict

        Parameter names mapped to their values.

  property kernel_

    Return the kernel of the base estimator.

  log_marginal_likelihood(theta=None, eval_gradient=False, clone_kernel=True)

    Return log-marginal likelihood of theta for training data.

    In the case of multi-class classification, the mean log-marginal likelihood of the one-versus-rest classifiers are returned.

    Parameters:
      theta array-like of shape (n_kernel_params,), default=None

        Kernel hyperparameters for which the log-marginal likelihood is evaluated. In the case of multi-class classification, theta may be the hyperparameters of the compound kernel or of an individual kernel. In the latter case, all individual kernel get assigned the same theta values. 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. Note that gradient computation is not supported for non-binary classification. 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 ndarray of 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.

  predict(X)

    Perform classification on an array of test vectors X.

    Parameters:
      X array-like of shape (n_samples, n_features) or list of object

        Query points where the GP is evaluated for classification.

    Returns:
      C ndarray of shape (n_samples,)

        Predicted target values for X, values are from `classes_`.

  predict_proba(X)

    Return probability estimates for the test vector X.

    Parameters:
      X array-like of shape (n_samples, n_features) or list of object

        Query points where the GP is evaluated for classification.

    Returns:
      C array-like of shape (n_samples, n_classes)

        Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

  score(X, y, sample_weight=None)

    Return the mean accuracy on the given test data and labels.

    In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

    Parameters:
      X array-like of shape (n_samples, n_features)

        Test samples.

      y array-like of shape (n_samples,) or (n_samples, n_outputs)

        True labels for `X`.

      sample_weight array-like of shape (n_samples,), default=None

        Sample weights.

    Returns:
      score float

        Mean accuracy of `self.predict(X)` w.r.t. `y`.

  set_params(**params)

    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.

    Parameters:
      **params dict

        Estimator parameters.

    Returns:
      self estimator instance

        Estimator instance.

  set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → GaussianProcessClassifier

    Request metadata passed to the `score` method.

    Note that this method is only relevant if `enable_metadata_routing=True` (see `sklearn.set_config`). Please see 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.

    New in version 1.3.

    Note

    This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a `Pipeline`. Otherwise it has no effect.

    Parameters:
      sample_weight str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

        Metadata routing for `sample_weight` parameter in `score`.

    Returns:
      self object

        The updated object.

## Examples using `sklearn.gaussian_process.GaussianProcessClassifier`

Classifier comparison

Classifier comparison

Plot classification probability

Plot classification probability

Gaussian process classification (GPC) on iris dataset

Gaussian process classification (GPC) on iris dataset

Gaussian processes on discrete data structures

Gaussian processes on discrete data structures

Illustration of Gaussian process classification (GPC) on the XOR dataset

Illustration of Gaussian process classification (GPC) on the XOR dataset

Iso-probability lines for Gaussian Processes classification (GPC)

Iso-probability lines for Gaussian Processes classification (GPC)

Probabilistic predictions with Gaussian process classification (GPC)

Probabilistic predictions with Gaussian process classification (GPC)