Implementation:Scikit learn Scikit learn GPKernels
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Gaussian Processes, Kernel Methods |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete implementations of kernel functions for Gaussian process regression and classification provided by scikit-learn.
Description
The kernels module provides a rich set of kernel functions that can be combined via "+" and "*" operators or exponentiated with "**". The abstract Kernel base class defines the interface. Key kernels include RBF (squared exponential), Matern, ConstantKernel, WhiteKernel, DotProduct, RationalQuadratic, and ExpSineSquared. All kernels support analytic gradient-based hyperparameter optimization via the Hyperparameter namedtuple.
Usage
Use these kernels when configuring Gaussian process models. Combine kernels to model complex covariance structures (e.g., periodic + smooth trends). Hyperparameters are automatically optimized during GP fitting unless their bounds are set to "fixed".
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/gaussian_process/kernels.py
Signature
class Hyperparameter(namedtuple("Hyperparameter",
("name", "value_type", "bounds", "n_elements", "fixed"))):
...
class Kernel(metaclass=ABCMeta):
def __call__(self, X, Y=None, eval_gradient=False):
...
def __add__(self, b):
...
def __mul__(self, b):
...
def __pow__(self, b):
...
class ConstantKernel(StationaryKernelMixin, GenericKernelMixin, Kernel):
def __init__(self, constant_value=1.0, constant_value_bounds=(1e-5, 1e5)):
...
class RBF(StationaryKernelMixin, NormalizedKernelMixin, Kernel):
def __init__(self, length_scale=1.0, length_scale_bounds=(1e-5, 1e5)):
...
class Matern(RBF):
def __init__(self, length_scale=1.0, length_scale_bounds=(1e-5, 1e5), nu=1.5):
...
class WhiteKernel(StationaryKernelMixin, GenericKernelMixin, Kernel):
def __init__(self, noise_level=1.0, noise_level_bounds=(1e-5, 1e5)):
...
class DotProduct(Kernel):
def __init__(self, sigma_0=1.0, sigma_0_bounds=(1e-5, 1e5)):
...
Import
from sklearn.gaussian_process.kernels import (
RBF, Matern, ConstantKernel, WhiteKernel, DotProduct, Kernel
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | array-like of shape (n_samples_X, n_features) | Yes | Left argument of the kernel |
| Y | array-like of shape (n_samples_Y, n_features) | No | Right argument (if None, K(X, X) is computed) |
| eval_gradient | bool | No | Whether to return gradient w.r.t. kernel hyperparameters |
| length_scale | float or ndarray | No | Length scale parameter (for RBF, Matern) |
| nu | float | No | Smoothness parameter for Matern kernel |
Outputs
| Name | Type | Description |
|---|---|---|
| K | ndarray of shape (n_samples_X, n_samples_Y) | Kernel matrix |
| K_gradient | ndarray of shape (n_samples_X, n_samples_X, n_dims) | Gradient of kernel matrix (if eval_gradient=True) |
Usage Examples
Basic Usage
import numpy as np
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C, WhiteKernel
# Compose a kernel: constant * RBF + noise
kernel = C(1.0) * RBF(length_scale=1.0) + WhiteKernel(noise_level=0.1)
print(kernel) # 1**2 * RBF(length_scale=1) + WhiteKernel(noise_level=0.1)
# Evaluate kernel matrix
X = np.array([[0], [1], [2]])
K = kernel(X)
print(K.shape) # (3, 3)