如何在 Sklearn 中为 Matern 内核指定 nu=infinity?

How to specify nu=infinity for Matern kernel in Sklearn?

我正在尝试在 Sklearn 中创建一个 Matern 内核,并将参数 nu 设置为无穷大。

from sklearn.gaussian_process.kernels import Matern
import numpy as np
kernel = Matern(nu=float('inf'))
a = np.random.randn(5)[:,np.newaxis]
b = a
kernel(a,b)

当我 运行 此代码时,我收到一条错误消息:

"RuntimeWarning: invalid value encountered in multiply
K *= tmp ** self.nu"

我还能如何在 Matern 内核中将参数 nu 指定为无穷大?谢谢

虽然the documentation seems to suggest that inf is a valid value for nu the code doesn't seem to account for that value, which is why you're seeing that error. However as the nu approaches infinity Matern kernel becomes equivalent to the squared exponential function, or the RBF在sklearn中,所以你可以使用下面的

from sklearn.gaussian_process.kernels import RBF
import numpy as np
kernel = RBF()
a = np.random.randn(5)[:,np.newaxis]
b = a
kernel(a,b)