我需要代码来进行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个随机点用于基准测试并被注释掉;这三点用于调试。
后一种情况的结果图是在这篇文章的最后。
以下是我的问题:
(请注意,在1000随机点情况下,FFT代码比SciPy代码快〜390倍。)
答案 0 :(得分:4)
您所看到的差异是由于带宽和缩放因素造成的,正如您已经注意到的那样。
默认情况下,如果您对细节感到好奇,gaussian_kde
会使用Scott's rule.挖掘into the code来选择带宽。下面的代码片段来自我写的quite awhile ago to do something similar到您正在做的事情。 (如果我没记错的话,那个特定版本有一个明显的错误,它确实不应该使用scipy.signal
进行卷积,但带宽估计和规范化是正确的。)
# 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)
卷积后,网格会被标准化:
# 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
希望这有助于澄清事情。
至于你的其他问题:
我可以避免直方图的.T和KDE1的fftshift吗?我 不知道为什么他们需要,但高斯人出现了错误 没有他们的地方。
我可能会误读您的代码,但我认为您只需要转置,因为您将从点坐标转到索引坐标(即从<x, y>
到<y, x>
)。
同样,为什么SciPy中的高斯人 即使我给了gaussian_kde,实现也不是径向对称的 标量带宽?
这是因为scipy使用输入x,y点的完全协方差矩阵来确定高斯核。您的公式假定x和y不相关。 gaussian_kde
测试并使用结果中x和y之间的相关性。
我如何实现SciPy中提供的其他带宽方法 对于FFT代码?
我会留下那个让你弄明白的。 :)但这并不难。基本上,不是scotts_factor
,而是更改公式并使用其他标量因子。其他一切都是一样的。