问题描述
这个想法是在沿 1D 散布的每个点上方堆叠一个内核.具体来说,内核的峰值与每个点的 x 轴点对齐/居中.这类似于
最终,将计算每个密度的总和,并生成如下所示的单一曲线(即灰线).
作为起点,我研究了
这符合您的要求吗?对不起,图表没有你的一半漂亮,但我认为这不是重点
将 numpy 导入为 np从 scipy.stats 导入规范# 定义一个半内核函数.如果需要,我们标准化为 integer(half_kernel) = 1def half_kernel(x, center, width = 1, normalize = True):kernel = (x>=center)*norm.pdf(x, center, width)如果标准化:内核 *= 2返回内核# 这是我们将内核居中的点——随机测试中心 = np.random.normal(0.0,2.0,7)# 我们查看结果的网格x = np.linspace(-3.0,3.0,101)# 在这里获取结果,每一列都是内核之一discr_kernels = np.zeros((len(x),len(centers)))对于范围内的 n(len(centers)):discr_kernels[:,n] = half_kernel(x, centre[n])y = discr_kernels.sum(axis= 1)plt.plot(x,discr_kernels,'--')plt.plot(x,y, '.-', label = 'total')plt.legend(loc = '最佳')
The idea is to stack a kernel above each point scattered along 1D. Specifically, the peak of the kernel is align/ centered to x-axis point each of the point. This is akin to Kernel Density Estimation except only half of the kernel is stacked at each of the point as shown at the picture below.
Ultimately, the summation of each of density will be calculated and will yield a single curve (i.e., gray line) as shown below.
As a starting point, I had dig around the Kernel Density Estimation of scikit learn module for an idea. However, I fail to find any line on how/where the stack they kernel on top each of point.
Really appreciate if someone can provide a good reading material for me to achieve this objective.
Does this do what you want? Sorry the graphs are not half as pretty as yours but did not think that was the point
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')
这篇关于如何使用Python在常规网格上的每个点上方堆叠内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!