def _compute_gaussian_kernel(histogram_shape, relative_bw):
"""Compute a gaussian kernel double the size of the histogram matrix"""
if len(histogram_shape) == 2:
kernel_shape = [2 * n for n in histogram_shape]
# Create a scaled grid in which the kernel is symmetric to avoid matrix
# inversion problems when the bandwiths are very different
bw_ratio = relative_bw[0] / relative_bw[1]
bw = relative_bw[0]
X, Y = np.mgrid[-bw_ratio:bw_ratio:kernel_shape[0] * 1j,
-1:1:kernel_shape[1] * 1j]
grid_points = np.vstack([X.ravel(), Y.ravel()]).T
Cov = np.array(((bw, 0), (0, bw)))**2
K = stats.multivariate_normal.pdf(grid_points, mean=(0, 0), cov=Cov)
return K.reshape(kernel_shape)
else:
grid = np.mgrid[-1:1:histogram_shape[0] * 2j]
return stats.norm.pdf(grid, loc=0, scale=relative_bw)
评论列表
文章目录