如果分别适当地选择每个函数中的sigma和bw_method参数,则对给定数据集应用函数
scipy.ndimage.filters.gaussian_filter和
scipy.stats.gaussian_kde可以给出非常类似的结果.
例如,我可以通过设置sigma = 2来获得以下图表的随机2D分布点.在gaussian_filter(左图)和bw_method = sigma / 30.在gaussian_kde(右图)中:
(MWE位于问题的最底部)
这些参数之间显然存在关系,因为一个应用高斯滤波器而另一个应用高斯核密度估计器.
每个参数的定义是:
sigma : scalar or sequence of scalars Standard deviation for Gaussian
kernel. The standard deviations of the Gaussian filter are given for
each axis as a sequence, or as a single number, in which case it is
equal for all axes.
鉴于高斯运算符的定义,我可以理解这个:
bw_method : str, scalar or callable, optional The method used to
calculate the estimator bandwidth. This can be ‘scott’, ‘silverman’, a
scalar constant or a callable. If a scalar, this will be used directly
as kde.factor. If a callable, it should take a gaussian_kde instance
as only parameter and return a scalar. If None (default), ‘scott’ is
used. See Notes for more details.
在这种情况下,让我们假设bw_method的输入是一个标量(浮点数),以便与sigma相媲美.这是我迷路的地方,因为我无法在任何地方找到有关此kde.factor参数的信息.
我想知道的是连接这些参数的精确数学方程式(例如:如果可能的话,使用浮点数时的sigma和bw_method).
MWE:
import numpy as np
from scipy.stats import gaussian_kde
from scipy.ndimage.filters import gaussian_filter
import matplotlib.pyplot as plt
def rand_data():
return np.random.uniform(low=1., high=200., size=(1000,))
# Generate 2D data.
x_data, y_data = rand_data(), rand_data()
xmin, xmax = min(x_data), max(x_data)
ymin, ymax = min(y_data), max(y_data)
# Define grid density.
gd = 100
# Define bandwidth
bw = 2.
# Using gaussian_filter
# Obtain 2D histogram.
rang = [[xmin, xmax], [ymin, ymax]]
binsxy = [gd, gd]
hist1, xedges, yedges = np.histogram2d(x_data, y_data, range=rang, bins=binsxy)
# Gaussian filtered histogram.
h_g = gaussian_filter(hist1, bw)
# Using gaussian_kde
values = np.vstack([x_data, y_data])
# Data 2D kernel density estimate.
kernel = gaussian_kde(values, bw_method=bw / 30.)
# Define x,y grid.
gd_c = complex(0, gd)
x, y = np.mgrid[xmin:xmax:gd_c, ymin:ymax:gd_c]
positions = np.vstack([x.ravel(), y.ravel()])
# Evaluate KDE.
z = kernel(positions)
# Re-shape for plotting
z = z.reshape(gd, gd)
# Make plots.
fig, (ax1, ax2) = plt.subplots(1, 2)
# Gaussian filtered 2D histograms.
ax1.imshow(h_g.transpose(), origin='lower')
ax2.imshow(z.transpose(), origin='lower')
plt.show()