GPy - Gaussian Process Toolbox
GPy is a Gaussian process (GP) framework written in Python, designed for flexible and robust GP modeling. It provides a comprehensive suite of tools for defining, manipulating, and optimizing Gaussian process models, including various kernels, likelihoods, and inference methods. The current version is 1.13.2, with releases occurring periodically, often tied to maintenance or feature additions rather than a strict schedule.
Common errors
-
AttributeError: module 'GPy' has no attribute 'GPRegression'
cause Attempting to access model classes like `GPRegression` directly from the top-level `GPy` module.fix`GPRegression` is located within the `GPy.models` submodule. Use `GPy.models.GPRegression`. -
GPy plotting requires matplotlib to be installed.
cause `matplotlib` is a soft dependency for plotting functionality and might not be installed by default.fixInstall `matplotlib` using `pip install matplotlib`. -
ValueError: Inputs must be 2-dimensional. (e.g. X.shape = (N, D))
cause GPy typically expects input data `X` and output data `Y` to be 2-dimensional arrays (e.g., `(N, 1)` for 1D input/output), not 1D arrays.fixReshape your 1D data arrays using `.reshape(-1, 1)`. For example, `X = X.reshape(-1, 1)` and `Y = Y.reshape(-1, 1)`. -
Optimize failed to converge
cause The optimization algorithm (typically a gradient-based method) could not find a stable minimum within the allotted iterations, often due to poor initial parameter values, local optima, or numerical instability.fixIncrease the `max_iters` parameter in `m.optimize()`. Consider using `m.optimize_restarts(num_restarts=N)` to run optimization from multiple random starting points to find a better minimum. Inspect your kernel choice and data for potential issues.
Warnings
- breaking GPy no longer supports Python 2.x. It explicitly requires Python 3.9 or newer.
- gotcha Optimization (`m.optimize()`) does not guarantee finding the global optimum for complex models and datasets, and can get stuck in local minima or fail to converge.
- gotcha GPy's plotting capabilities rely on `matplotlib`. Issues may arise if `matplotlib` is not installed, or if there are backend conflicts in non-interactive environments.
- gotcha GPy's dependency on `numpy` and `scipy` means that version mismatches can sometimes lead to unexpected errors or deprecation warnings, especially with very new `numpy`/`scipy` releases and older GPy versions.
Install
-
pip install gpy
Imports
- GPy
import GPy
- GPRegression
from GPy import GPRegression
import GPy; model = GPy.models.GPRegression(...)
- RBF
from GPy import RBF
import GPy; kernel = GPy.kern.RBF(...)
Quickstart
import GPy import numpy as np # 1. Generate some synthetic data X = np.random.uniform(-3., 3., (20, 1)) Y = np.sin(X) + np.random.randn(20, 1) * 0.05 # 2. Define a kernel (e.g., Radial Basis Function) kernel = GPy.kern.RBF(input_dim=1, variance=1., lengthscale=1.) # 3. Create a GP regression model m = GPy.models.GPRegression(X, Y, kernel) # 4. Optimize the model's hyperparameters m.optimize(messages=True, max_iters=100) # Print optimized parameters print(m) # To plot (requires matplotlib): # import matplotlib.pyplot as plt # m.plot() # plt.show()