Gaussian Process regression implementation.
Classes
class algorithms.bayesian.gaussian_processes.GaussianProcessesRegressor(Regressor)
Gaussian Process Regression (GPR) with uncertainty estimation.
__init__( self, kernel: str = 'rbf', gamma: Any = 'scale', degree: int = 2, noise: float = 1.0, normalize: bool = True, )
Overview
The algorithm performs regression through the following steps:
- Optionally normalise features and targets to zero mean, unit variance
- Compute the kernel matrix K between all training points
- Add noise to the diagonal for regularisation: K + \sigma_n^2 I
- Solve for dual coefficients via Cholesky decomposition
- At prediction time, compute the predictive mean and variance
Theory
A Gaussian Process defines a distribution over functions:
Given training data (X, \mathbf{y}) with noise \sigma_n^2, the predictive distribution at test points X_* is Gaussian:
The log marginal likelihood used for model comparison is:
where K_y = K + \sigma_n^2 I.
Parameters
kernel
The kernel function to use:
-
'rbf'- Radial Basis Function (Squared Exponential) -
'poly'- Polynomial kernel -
'linear'- Linear (dot product) kernel
gamma
1.0 / (n_features * X.var()).
degree
noise
normalize
Attributes
alpha_
L_
X_train_
normalize=True).
y_train_
normalize=True).
y_mean_
y_std_
Notes
Complexity:
- Training: O(n^3) due to Cholesky decomposition of the n \times n kernel matrix
- Prediction: O(n^2) per test point for the mean; O(n^2) for the variance
- Memory: O(n^2) for storing the kernel matrix
- When you need calibrated uncertainty estimates alongside predictions
- Small-to-medium datasets (up to a few thousand samples)
- When the relationship between features and target is smooth
- Bayesian optimisation and active learning settings
- When model selection via the log marginal likelihood is desirable
References
Basic regression with RBF kernel:
>>> from tuiml.algorithms.bayesian import GaussianProcessesRegressor
>>> import numpy as np
>>>
>>> # Create sinusoidal training data
>>> X = np.array([[1], [3], [5], [7], [9]])
>>> y = np.sin(X).ravel()
>>>
>>> # Fit the Gaussian Process model
>>> gp = GaussianProcessesRegressor(kernel='rbf', noise=0.1)
>>> gp.fit(X, y)
GaussianProcessesRegressor(kernel='rbf', noise=0.1, n_train=5)
>>>
>>> # Predict with uncertainty
>>> mean, std = gp.predict([[4]], return_std=True)
Methods
fit
(self, X: np.ndarray, y: np.ndarray) -> 'GaussianProcessesRegressor'
fit
(self, X: np.ndarray, y: np.ndarray) -> 'GaussianProcessesRegressor'
Fit the Gaussian Process regression model.
Parameters
X
y
Returns
self
predict
(self, X: np.ndarray, return_std: bool=False) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]
predict
(self, X: np.ndarray, return_std: bool=False) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]
Predict using the Gaussian Process regression model.
Parameters
X
return_std
Returns
y_mean
y_std
return_std is True.
predict_proba
(self, X: np.ndarray) -> Tuple[np.ndarray, np.ndarray]
predict_proba
(self, X: np.ndarray) -> Tuple[np.ndarray, np.ndarray]
Predict with uncertainty estimates.
Parameters
X
Returns
mean
std
score
(self, X: np.ndarray, y: np.ndarray) -> float
score
(self, X: np.ndarray, y: np.ndarray) -> float
Compute the R-squared (coefficient of determination) score.
Parameters
X
y
Returns
r2