在python中实现基于FFT的2D内核密度估计器,并将其与SciPy实施方式进行比较

石匠

我需要执行2D内核密度估计(KDE)的代码,并且我发现SciPy实现太慢。因此,我已经编写了一个基于FFT的实现,但是有几件事使我感到困惑。(FFT实现还强制执行周期性边界条件,这正是我想要的。)

该实现基于从样本创建一个简单的直方图,然后将其与高斯进行卷积。这是执行此操作的代码,并将其与SciPy结果进行比较。

from numpy import *
from scipy.stats import *
from numpy.fft import *
from matplotlib.pyplot import *
from time import clock

ion()

#PARAMETERS
N   = 512   #number of histogram bins; want 2^n for maximum FFT speed?
nSamp   = 1000  #number of samples if using the ranom variable
h   = 0.1   #width of gaussian
wh  = 1.0   #width and height of square domain

#VARIABLES FROM PARAMETERS
rv  = uniform(loc=-wh,scale=2*wh)   #random variable that can generate samples
xyBnds  = linspace(-1.0, 1.0, N+1)  #boundaries of histogram bins
xy  = (xyBnds[1:] + xyBnds[:-1])/2      #centers of histogram bins
xx, yy = meshgrid(xy,xy)

#DEFINE SAMPLES, TWO OPTIONS
#samples = rv.rvs(size=(nSamp,2))
samples = array([[0.5,0.5],[0.2,0.5],[0.2,0.2]])

#DEFINITIONS FOR FFT IMPLEMENTATION
ker = exp(-(xx**2 + yy**2)/2/h**2)/h/sqrt(2*pi) #Gaussian kernel
fKer = fft2(ker) #DFT of kernel

#FFT IMPLEMENTATION
stime = clock()
#generate normalized histogram. Note sure why .T is needed:
hst = histogram2d(samples[:,0], samples[:,1], bins=xyBnds)[0].T / (xy[-1] - xy[0])**2
#convolve histogram with kernel. Not sure why fftshift is neeed:
KDE1 = fftshift(ifft2(fft2(hst)*fKer))/N
etime = clock()
print "FFT method time:", etime - stime

#DEFINITIONS FOR NON-FFT IMPLEMTATION FROM SCIPY
#points to sample the KDE at, in a form gaussian_kde likes:
grid_coords = append(xx.reshape(-1,1),yy.reshape(-1,1),axis=1)

#NON-FFT IMPLEMTATION FROM SCIPY
stime = clock()
KDEfn = gaussian_kde(samples.T, bw_method=h)
KDE2 = KDEfn(grid_coords.T).reshape((N,N))
etime = clock()
print "SciPy time:", etime - stime

#PLOT FFT IMPLEMENTATION RESULTS
fig = figure()
ax = fig.add_subplot(111, aspect='equal')
c = contour(xy, xy, KDE1.real)
clabel(c)
title("FFT Implementation Results")

#PRINT SCIPY IMPLEMENTATION RESULTS
fig = figure()
ax = fig.add_subplot(111, aspect='equal')
c = contour(xy, xy, KDE2)
clabel(c)
title("SciPy Implementation Results")

上面有两组样本。1000个随机点用于基准测试并被注释掉;这三点用于调试。

后一种情况的结果图在此帖子的结尾。

这是我的问题:

  • 我可以避免直方图的.T和KDE1的fftshift吗?我不确定为什么需要它们,但是没有它们,高斯人会出现在错误的地方。
  • 如何为SciPy定义标量带宽?在两种实现中,高斯的宽度有很大不同。
  • 同样,即使我给gaussian_kde标量带宽,SciPy实现中的高斯为何也不径向对称?
  • 如何为FFT代码实现SciPy中可用的其他带宽方法?

(请注意,在1000个随机点的情况下,FFT码比SciPy码快390倍。)

使用FFT进行三点调试KDE。 使用SciPy进行三点调试KDE

乔·金顿

正如您已经注意到的,您看到的差异是由于带宽和缩放因子所致。

By default, gaussian_kde chooses the bandwidth using Scott's rule. Dig into the code, if you're curious about the details. The code snippets below are from something I wrote quite awhile ago to do something similar to what you're doing. (If I remember right, there's an obvious error in that particular version and it really shouldn't use scipy.signal for the convolution, but the bandwidth estimation and normalization are correct.)

# Calculate the covariance matrix (in pixel coords)
cov = np.cov(xyi)

# Scaling factor for bandwidth
scotts_factor = np.power(n, -1.0 / 6) # For 2D

#---- Make the gaussian kernel -------------------------------------------

# First, determine how big the gridded kernel needs to be (2 stdev radius) 
# (do we need to convolve with a 5x5 array or a 100x100 array?)
std_devs = np.diag(np.sqrt(cov))
kern_nx, kern_ny = np.round(scotts_factor * 2 * np.pi * std_devs)

# Determine the bandwidth to use for the gaussian kernel
inv_cov = np.linalg.inv(cov * scotts_factor**2) 

After the convolution, the grid is then normalized:

# Normalization factor to divide result by so that units are in the same
# units as scipy.stats.kde.gaussian_kde's output.  (Sums to 1 over infinity)
norm_factor = 2 * np.pi * cov * scotts_factor**2
norm_factor = np.linalg.det(norm_factor)
norm_factor = n * dx * dy * np.sqrt(norm_factor)

# Normalize the result
grid /= norm_factor

Hopefully that helps clarify things a touch.

As for your other questions:

Can I avoid the .T for the histogram and the fftshift for KDE1? I'm not sure why they're needed, but the gaussians show up in the wrong places without them.

I could be misreading your code, but I think you just have the transpose because you're going from point coordinates to index coordinates (i.e. from <x, y> to <y, x>).

Along the same lines, why are the gaussians in the SciPy implementation not radially symmetric even though I gave gaussian_kde a scalar bandwidth?

This is because scipy uses the full covariance matrix of the input x, y points to determine the gaussian kernel. Your formula assumes that x and y aren't correlated. gaussian_kde tests for and uses the correlation between x and y in the result.

How could I implement the other bandwidth methods available in SciPy for the FFT code?

我把那个留给你找出来。:)并不是很难。基本上,代替scotts_factor,您可以更改公式并具有其他一些标量因子。其他一切都一样。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用sklearn在2D图像/阵列上获得内核密度估计器?

来自分类Dev

使用Python绘制2D内核密度估计图

来自分类Dev

使用Seaborn为python中的matplotlib进行内核密度估计的下限

来自分类Dev

使用Seaborn为python中的matplotlib进行内核密度估计的下限

来自分类Dev

Python中的多变量内核密度估计

来自分类Dev

Python中的多处理空间内核密度估计

来自分类Dev

R中的核密度估计器

来自分类Dev

如何估计2D GMM及其梯度的核密度估计?

来自分类Dev

一张d3.js图表中的多个内核密度估计

来自分类Dev

R中的密度估计

来自分类Dev

Python - SciPy 内核估计示例 - 密度 >> 1

来自分类Dev

python中的加权高斯核密度估计

来自分类Dev

在numpy中创建密度估计

来自分类Dev

R中的高斯核密度估计

来自分类Dev

在numpy中创建密度估计

来自分类Dev

从 sklearn 核密度估计中采样

来自分类Dev

将内核密度估计值提取到R中的新采样点

来自分类Dev

如何在R中重用我的内核密度估计函数?

来自分类Dev

如何使用scikit归一化内核密度估计?

来自分类Dev

基于核密度估计的PDF近似优化计算时间

来自分类Dev

在density2d中计算密度估计值?

来自分类Dev

从 R 中的 3D 核密度估计和提取点值

来自分类Dev

如何将内核密度估计作为scikit中的一维聚类方法来学习?

来自分类Dev

2D内核密度e。在python中-x轴拥挤和缩小

来自分类Dev

2D内核密度e。在python中-x轴拥挤和缩小

来自分类Dev

在Python中用于条件密度估计的工具

来自分类Dev

如何获取stats :: density中特定值的密度估计?

来自分类Dev

Matlab中的knn(k最近邻)密度估计源

来自分类Dev

Matlab中的knn(k最近邻)密度估计源

Related 相关文章

热门标签

归档