Ability of Gaussian process regression (GPR) to estimate data noise-level
This example shows the ability of the
WhiteKernel to estimate the noise
level in the data. Moreover, we show the importance of kernel hyperparameters
initialization.
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clause
Data generation
We will work in a setting where X will contain a single feature. We create a
function that will generate the target to be predicted. We will add an
option to add some noise to the generated target.
The target is transforming the input X using a sine function. Now, we will
generate few noisy training samples. To illustrate the noise level, we will
plot the true signal together with the noisy training samples.
Now, we will create a
GaussianProcessRegressor
using an additive kernel adding a
RBF and
WhiteKernel kernels.
The WhiteKernel is a kernel that
will able to estimate the amount of noise present in the data while the
RBF will serve at fitting the
non-linearity between the data and the target.
However, we will show that the hyperparameter space contains several local
minima. It will highlights the importance of initial hyperparameter values.
We will create a model using a kernel with a high noise level and a large
length scale, which will explain all variations in the data by noise.
/home/circleci/project/sklearn/gaussian_process/kernels.py:455: ConvergenceWarning: The optimal value found for dimension 0 of parameter k1__k2__length_scale is close to the specified upper bound 1000.0. Increasing the bound and calling fit again may find a better value.
warnings.warn(
We see that the optimum kernel found still has a high noise level and an even
larger length scale. The length scale reaches the maximum bound that we
allowed for this parameter and we got a warning as a result.
More importantly, we observe that the model does not provide useful
predictions: the mean prediction seems to be constant: it does not follow the
expected noise-free signal.
Now, we will initialize the RBF
with a larger length_scale initial value and the
WhiteKernel with a smaller initial
noise level lower while keeping the parameter bounds unchanged.
First, we see that the model’s predictions are more precise than the
previous model’s: this new model is able to estimate the noise-free
functional relationship.
Looking at the kernel hyperparameters, we see that the best combination found
has a smaller noise level and shorter length scale than the first model.
We can inspect the negative Log-Marginal-Likelihood (LML) of
GaussianProcessRegressor
for different hyperparameters to get a sense of the local minima.
We see that there are two local minima that correspond to the combination of
hyperparameters previously found. Depending on the initial values for the
hyperparameters, the gradient-based optimization might or might not
converge to the best model. It is thus important to repeat the optimization
several times for different initializations. This can be done by setting the
n_restarts_optimizer parameter of the
GaussianProcessRegressor class.
Let’s try again to fit our model with the bad initial values but this time
with 10 random restarts.