SKLearner Home | About | Contact | Examples

Scikit-Learn Gaussian Process with "ConstantKernel" Kernel

Gaussian Process (GP) is a probabilistic model commonly used for regression and classification tasks. It is particularly useful when working with small datasets or when a measure of uncertainty is needed for the predictions.

The ConstantKernel is a simple covariance function used in GP that outputs a constant value. This kernel is useful when modeling a constant bias or offset in the data, and it has a single hyperparameter called constant_value. Typical values for constant_value are small positive numbers like 1.0 or 0.1.

GP with ConstantKernel is suitable for regression problems where a constant bias or offset needs to be modeled.

from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ConstantKernel
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np

# Prepare a synthetic 1D dataset with a constant bias
X = np.linspace(0, 10, 100).reshape(-1, 1)
y = 2.5 + np.random.normal(0, 1, (100, 1))

# Create an instance of GaussianProcessRegressor with ConstantKernel
kernel = ConstantKernel(constant_value=1.0)
gp = GaussianProcessRegressor(kernel=kernel, random_state=0)

# Split the dataset into train and test portions
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Fit the model on the training data
gp.fit(X_train, y_train)

# Evaluate the model's performance using mean squared error
y_pred = gp.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")

# Make a prediction using the fitted model on a test sample
test_sample = np.array([[5.0]])
pred = gp.predict(test_sample)
print(f"Predicted value for test sample: {pred[0]:.2f}")
Mean Squared Error: 9.42
Predicted value for test sample: 4.00

The key steps in this code example are:

  1. Dataset preparation: A synthetic 1D dataset is generated with a constant bias and some random noise.

  2. Model instantiation and configuration: An instance of GaussianProcessRegressor is created with the ConstantKernel, and the constant_value hyperparameter is set.

  3. Model training: The dataset is split into train and test portions, and the model is fitted on the training data.

  4. Model evaluation: The model’s performance is evaluated using mean squared error on the test set.

  5. Inference on test sample(s): A prediction is made using the fitted model on one test sample, demonstrating how the model can be used for inference on new data.



See Also