例如,我可以通过在gaussian_filter(左图)中设置sigma=2.和在{}(右绘图)中设置bw_method=sigma/30.来获得以下绘图:
(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的输入是标量(float),以便与sigma相比较。这里是我迷路的地方,因为我在任何地方都找不到关于这个kde.factor参数的信息。在
我想知道的是,如果可能的话,连接这两个参数的精确数学方程(即:当使用float时,sigma和{})。在
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()