Reputation: 444
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
where is the Matern covariance function and
,
are rows of the
locs
array. What I would like is to calculate 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
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