如何使用 Python 在规则网格上的每个点上方堆叠内核?
How to stack a kernel above each point on a regular grid with Python?
想法是在沿 1D 散布的每个点上方堆叠一个内核。具体来说,内核的峰值与每个点的 x 轴点对齐/居中。这类似于 Kernel Density Estimation 除了只有一半的内核堆叠在每个点上,如下图所示。
最终,将计算每个密度的总和,并将产生一条曲线(即灰线),如下所示。
作为起点,我围绕 scikit 学习模块的内核密度估计进行了挖掘以寻找一个想法。但是,我无法在 how/where 堆栈上找到任何行,它们位于每个点的顶部。
如果有人能为我提供好的阅读材料material,我将不胜感激objective。
这是否符合您的要求?抱歉,图表没有你的一半漂亮,但我认为这不是重点
import numpy as np
from scipy.stats import norm
# define a half-kernel function. We normalize to have integral(half_kernel) = 1 if required
def half_kernel(x, center, width = 1, normalize = True):
kernel = (x>=center)*norm.pdf(x, center, width)
if normalize:
kernel *= 2
return kernel
# this are the points where we center our kernels -- random for testing
centers = np.random.normal(0.0,2.0,7)
# Grid on which we look at the results
x = np.linspace(-3.0,3.0,101)
# get the results here, each column is one of the kernels
discr_kernels = np.zeros((len(x),len(centers)))
for n in range(len(centers)):
discr_kernels[:,n] = half_kernel(x, centers[n])
y = discr_kernels.sum(axis= 1)
plt.plot(x,discr_kernels,'--')
plt.plot(x,y, '.-', label = 'total')
plt.legend(loc = 'best')
想法是在沿 1D 散布的每个点上方堆叠一个内核。具体来说,内核的峰值与每个点的 x 轴点对齐/居中。这类似于 Kernel Density Estimation 除了只有一半的内核堆叠在每个点上,如下图所示。
最终,将计算每个密度的总和,并将产生一条曲线(即灰线),如下所示。
作为起点,我围绕 scikit 学习模块的内核密度估计进行了挖掘以寻找一个想法。但是,我无法在 how/where 堆栈上找到任何行,它们位于每个点的顶部。
如果有人能为我提供好的阅读材料material,我将不胜感激objective。
这是否符合您的要求?抱歉,图表没有你的一半漂亮,但我认为这不是重点
import numpy as np
from scipy.stats import norm
# define a half-kernel function. We normalize to have integral(half_kernel) = 1 if required
def half_kernel(x, center, width = 1, normalize = True):
kernel = (x>=center)*norm.pdf(x, center, width)
if normalize:
kernel *= 2
return kernel
# this are the points where we center our kernels -- random for testing
centers = np.random.normal(0.0,2.0,7)
# Grid on which we look at the results
x = np.linspace(-3.0,3.0,101)
# get the results here, each column is one of the kernels
discr_kernels = np.zeros((len(x),len(centers)))
for n in range(len(centers)):
discr_kernels[:,n] = half_kernel(x, centers[n])
y = discr_kernels.sum(axis= 1)
plt.plot(x,discr_kernels,'--')
plt.plot(x,y, '.-', label = 'total')
plt.legend(loc = 'best')