在 Python 中实现离散高斯核?
Implementing Discrete Gaussian Kernel in Python?
我希望实现 Lindeberg 在他关于规模 space 理论的工作中所定义的 discrete Gaussian kernel。
它被定义为 T(n,t) = exp(-t)*I_n(t) 其中 I_n 是 modified Bessel function of the first kind.
我正在尝试使用 Numpy 和 Scipy 在 Python 中实现此功能,但 运行 遇到了一些麻烦。
def discrete_gaussian_kernel(t, n):
return math.exp(-t) * scipy.special.iv(n, t)
我尝试绘图:
import math
import numpy as np
import scipy
from matplotlib import pyplot as plt
def kernel(t, n):
return math.exp(-t) * scipy.special.iv(n, t)
ns = np.linspace(-5, 5, 1000)
y0 = discrete_gaussian_kernel(0.5, ns)
y1 = discrete_gaussian_kernel(1, ns)
y2 = discrete_gaussian_kernel(2, ns)
y3 = discrete_gaussian_kernel(4, ns)
plt.plot(ns, y0, ns, y1, ns, y2, ns, y3)
plt.xlim([-4, 4])
plt.ylim([0, 0.7])
输出如下:
根据维基百科文章,它应该是这样的:
我想我犯了一些非常微不足道的错误。 :/ 有什么想法吗?
谢谢!
编辑:
我写的相当于scipy.special.ive(n, t)
。我很确定它应该是第一类的修正贝塞尔函数,而不是第二类,但是有人可以确认吗?
如果你想获取维基百科情节,替换
ns = np.linspace(-5, 5, 1000)
与
ns = np.arange(-5, 5+1)
即,您使用的公式仅在整数点上有意义。
作为负阶函数的贝塞尔函数是一个振荡函数,http://dlmf.nist.gov/10.27#E2 所以我觉得情节很好。
我希望实现 Lindeberg 在他关于规模 space 理论的工作中所定义的 discrete Gaussian kernel。
它被定义为 T(n,t) = exp(-t)*I_n(t) 其中 I_n 是 modified Bessel function of the first kind.
我正在尝试使用 Numpy 和 Scipy 在 Python 中实现此功能,但 运行 遇到了一些麻烦。
def discrete_gaussian_kernel(t, n):
return math.exp(-t) * scipy.special.iv(n, t)
我尝试绘图:
import math
import numpy as np
import scipy
from matplotlib import pyplot as plt
def kernel(t, n):
return math.exp(-t) * scipy.special.iv(n, t)
ns = np.linspace(-5, 5, 1000)
y0 = discrete_gaussian_kernel(0.5, ns)
y1 = discrete_gaussian_kernel(1, ns)
y2 = discrete_gaussian_kernel(2, ns)
y3 = discrete_gaussian_kernel(4, ns)
plt.plot(ns, y0, ns, y1, ns, y2, ns, y3)
plt.xlim([-4, 4])
plt.ylim([0, 0.7])
输出如下:
根据维基百科文章,它应该是这样的:
我想我犯了一些非常微不足道的错误。 :/ 有什么想法吗? 谢谢!
编辑:
我写的相当于scipy.special.ive(n, t)
。我很确定它应该是第一类的修正贝塞尔函数,而不是第二类,但是有人可以确认吗?
如果你想获取维基百科情节,替换
ns = np.linspace(-5, 5, 1000)
与
ns = np.arange(-5, 5+1)
即,您使用的公式仅在整数点上有意义。 作为负阶函数的贝塞尔函数是一个振荡函数,http://dlmf.nist.gov/10.27#E2 所以我觉得情节很好。