marcin_j
marcin_j

Reputation: 444

evaluate the Matern kernel with custom distances

I'm trying to evaluate the matern kernel from the scikit learn package. So basically, I do something like this

import numpy as np
from sklearn.gaussian_process.kernels import Matern as skMatern

locs=np.array([[0, 0], [0, 0.5], [0.5, 0.7]])

K = skMatern(nu=1.8, length_scale=0.2)
covMat = K(locs)

My problem is that I want to be able to determine the way the kernel calculates distances between the points.

In other words, the code I have above calculates

enter image description here

where enter image description here is the Matern covariance function and enter image description here, enter image description here are rows of the locs array. What I would like is to calculate enter image description here and be able to specify d myself (maybe as a distance matrix). I've been looking at the scikit-learn documentation but haven't been able to figure out how to do it. Thanks for your help!

Upvotes: 4

Views: 1163

Answers (1)

cyril
cyril

Reputation: 609

A bit late, but it could help someone else. If d is your distance matrix, you can write :

import numpy as np
from sklearn.gaussian_process.kernels import Matern
from sklearn.metrics import pairwise_distances

m, n = 10, 3
d = pairwise_distances(np.random.randn(m, n))

K = Matern()(0, d.ravel()[:, np.newaxis]).reshape(d.shape)

This line of code benefits from the fact that 0 is broadcasted to the right shape.

Upvotes: 1

Related Questions