Implementation of Gaussian Process in Python
Scikit Learn
import matplotlib.pyplot as plt
import numpy as np
from scipy import linalg
from sklearn.gaussian_process import kernels,GaussianProcessRegressor
## check version
import sys
import sklearn
print(sys.version)
!python --version
print("numpy:", np.__version__)
print("sklearn:",sklearn.__version__)
The necessary libraries for Gaussian Process Regression (GPR) in Python are imported by this code; these are SciPy for linear algebra functions, NumPy for numerical operations, and Matplotlib for data visualization. To make sure it is compatible with the necessary packages, it additionally verifies the version of Python and prints it, along with the versions of NumPy and scikit-learn (sklearn).
Kernel Selection
np.random.seed(0)
n=50
kernel_ =[kernels.RBF (),
kernels.RationalQuadratic(),
kernels.ExpSineSquared(periodicity=10.0),
kernels.DotProduct(sigma_0=1.0)**2,
kernels.Matern()
]
print(kernel_, '\n')
Output:
[RBF(length_scale=1),
RationalQuadratic(alpha=1, length_scale=1),
ExpSineSquared(length_scale=1, periodicity=10),
DotProduct(sigma_0=1) ** 2,
Matern(length_scale=1, nu=1.5)]
The code specifies the number of test sites (n) and initializes a random seed. In order to display the chosen kernels, it generates a list of several kernel functions and prints the list.
Kernel Comparison and Visualization
for kernel in kernel_:
# Gaussian process
gp = GaussianProcessRegressor(kernel=kernel)
# Prior
x_test = np.linspace(-5, 5, n).reshape(-1, 1)
mu_prior, sd_prior = gp.predict(x_test, return_std=True)
samples_prior = gp.sample_y(x_test, 3)
# plot
plt.figure(figsize=(10, 3))
plt.subplot(1, 2, 1)
plt.plot(x_test, mu_prior)
plt.fill_between(x_test.ravel(), mu_prior - sd_prior,
mu_prior + sd_prior, color='aliceblue')
plt.plot(x_test, samples_prior, '--')
plt.title('Prior')
# Fit
x_train = np.array([-4, -3, -2, -1, 1]).reshape(-1, 1)
y_train = np.sin(x_train)
gp.fit(x_train, y_train)
# posterior
mu_post, sd_post = gp.predict(x_test, return_std=True)
mu_post = mu_post.reshape(-1)
samples_post = np.squeeze(gp.sample_y(x_test, 3))
# plot
plt.subplot(1, 2, 2)
plt.plot(x_test, mu_post)
plt.fill_between(x_test.ravel(), mu_post - sd_post,
mu_post + sd_post, color='aliceblue')
plt.plot(x_test, samples_post, '--')
plt.scatter(x_train, y_train, c='blue', s=50)
plt.title('Posterior')
plt.show()
print("gp.kernel_", gp.kernel_)
print("gp.log_marginal_likelihood:",
gp.log_marginal_likelihood(gp.kernel_.theta))
print('-'*50, '\n\n')
Output:
RBF
gp.kernel_ RBF(length_scale=1.93)
gp.log_marginal_likelihood: -3.444937833462133
---------------------------------------------------
Rational Quadratic
gp.kernel_ RationalQuadratic(alpha=1e+05, length_scale=1.93)
gp.log_marginal_likelihood: -3.4449718909150966
--------------------------------------------------
ExpSineSquared
gp.kernel_ ExpSineSquared(length_scale=0.000524, periodicity=2.31e+04)
gp.log_marginal_likelihood: -3.4449381454930217
--------------------------------------------------
Dot Product
gp.kernel_ DotProduct(sigma_0=0.998) ** 2
gp.log_marginal_likelihood: -150204291.56018084
--------------------------------------------------
Matern
gp.kernel_ Matern(length_scale=1.99, nu=1.5)
gp.log_marginal_likelihood: -5.131637070524745
--------------------------------------------------
The code starts by looping over the various kernel functions listed in the kernel_ list. A Gaussian Process Regressor (gp) is made using the particular kernel for every kernel. For the Gaussian Process, this establishes the covariance structure. In order to assess the previous distribution, a set of test input points called x_test is established, with values ranging from -5 to 5. This set of points is transformed into a column vector.
Using the gp.predict method, the prior distribution’s mean (mu_prior) and standard deviation (sd_prior) are determined at each test point. Standard deviation values are requested using the return_std=True option. gp.sample_y(x_test, 3) is used to get three function samples from the previous distribution.
The first subplot shows the previous distribution’s mean, with the standard deviation represented by a shaded area. The samples are superimposed as dashed lines, while the mean is displayed as a solid line. There is a subplot called “Prior.” There is a defined set of training data points (x_train) and goal values (y_train) that go with them. The Gaussian Process model is fitted using these points (gp.fit(x_train, y_train)). Five data points with corresponding sine values make up the training data in this code.
Following the training data fitting phase, the procedure computes the posterior distribution’s mean (mu_post) and standard deviation (sd_post) for the same test points (x_test). gp.sample_y(x_test, 3) is also used to produce function samples from the posterior distribution. The second subplot overlays the sampled functions as dotted lines and shows the mean of the posterior distribution, shaded with the standard deviation. Plotted in blue are the training data points. The subplot has the name “Posterior.”
To see the previous and posterior plots for the current kernel and gain a visual understanding of the behavior of the model, call Matplotlib’s plt.show() function.
The code shows details about the current kernel, such as gp.kernel_, which indicates the current kernel being used, and gp.log_marginal_likelihood(gp.kernel_.theta), which gives the log marginal likelihood of the model using the current kernel, after each set of prior and posterior plots.
Advantages of Gaussian Process Regression (GPR)
Gaussian Process Regression (GPR) has a number of benefits in a range of applications:
- GPR provides a probabilistic framework for regression, which means it not only gives point estimates but also provides uncertainty estimates for predictions.
- It is highly flexible and can capture complex relationships in the data.
- GPR can be adapted to various applications, including time series forecasting, optimization, and Bayesian optimization.
Challenges of Gaussian Process Regression (GPR)
- GPR can be computationally expensive when dealing with large datasets, as the inversion of a covariance matrix is required.
- The choice of the kernel function and its hyperparameters can significantly impact the model’s performance.
Good Examples of GPR Applications
- Stock Price Prediction: GPR can be used to model and predict stock prices, taking into account the volatility and uncertainty in financial markets.
- Computer Experiments: GPR is useful in optimizing complex simulations by modeling the input-output relationships and identifying the most influential parameters.
- Anomaly Detection: GPR can be applied to anomaly detection, where it identifies unusual patterns in time series data by capturing normal data distributions.
Gaussian Process Regression (GPR)
Regression and probabilistic classification issues can be resolved using the Gaussian process (GP), a supervised learning technique. Since each Gaussian process can be thought of as an infinite-dimensional generalization of multivariate Gaussian distributions, the term “Gaussian” appears in the name. We will discuss Gaussian processes for regression in this post, which is also referred to as Gaussian process regression (GPR). Numerous real-world issues in the fields of materials science, chemistry, physics, and biology have been resolved with the use of GPR.
Table of Content
- Gaussian Process Regression (GPR)
- Key Concepts of Gaussian Process Regression (GPR)
- Mathematical Concept of Gaussian Process Regression (GPR)
- Implementation of Gaussian Process in Python