Search code examples
pythonscikit-learngaussiankriging

How to create anisotropic exponential and gaussian correlation function in Python for kernel?


I have a dataset of 1000 observed samples of 6 features that form the X and one target variable that forms the Y.

I am using kriging or Gaussian Process Regressor to train my models. I would like to use anisotropic Gaussian and anisotropic exponential correlation functions as kernels. Please see the attached equation snip for reference.

How do I define the named functions in python?

Equations describing the correlation function


Solution

  • Scikit-learn provides anisotropic gaussian and exponential kernels. All you have to do is replace the length scale with an array which has length equal to the number of features. In the case of gaussian kernel it works like this:

    from sklearn.datasets import load_iris
    from sklearn.gaussian_process import GaussianProcessClassifier
    from sklearn.gaussian_process.kernels import RBF,Matern
    X, y = load_iris(return_X_y=True)
    kernel = 1.0 * RBF([1.0,1.5,0.5,1]) # just create anisotropic kernel here
    #kernel = 1.0 * RBF([1]) # isotropic kernel
    gpc = GaussianProcessClassifier(kernel=kernel,random_state=0).fit(X, y)
    gpc.score(X, y)
    gpc.predict_proba(X[:2,:])
    

    You can obtain the exponential kernel as a special case of the Matern kernel. My maths are a bit rusty but Rasmussen&Williams Chapter 4 say this done by setting the nu parameter to 0.5:

    from sklearn.gaussian_process.kernels import Matern    
    kernel = 1.0 * Matern(length_scale=[1.0,1.5,0.5,1], nu=0.5)