sklearn中的2D KDE带宽与scipy中的带宽之间的关系

时间:2014-01-08 15:43:20

标签: python scipy scikit-learn

我正在尝试比较sklearn.neighbors.KernelDensityscipy.stats.gaussian_kde对二维数组的效果。

this article我看到每个函数中带宽(bw)的处理方式不同。本文给出了在scipy中设置正确bw的方法,因此它将等同于sklearn中使用的bw。基本上它将bw除以样本标准差。结果如下:

# For sklearn
bw = 0.15

# For scipy
bw = 0.15/x.std(ddof=1)

其中x是我用来获取KDE的示例数组。这在1D中工作得很好,但我无法在2D中工作。

这是我得到的MWE

import numpy as np
from scipy import stats
from sklearn.neighbors import KernelDensity

# Generate random data.
n = 1000
m1, m2 = np.random.normal(0.2, 0.2, size=n), np.random.normal(0.2, 0.2, size=n)
# Define limits.
xmin, xmax = min(m1), max(m1)
ymin, ymax = min(m2), max(m2)
# Format data.
x, y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([x.ravel(), y.ravel()])
values = np.vstack([m1, m2])

# Define some point to evaluate the KDEs.
x1, y1 = 0.5, 0.5

# -------------------------------------------------------
# Perform a kernel density estimate on the data using scipy.
kernel = stats.gaussian_kde(values, bw_method=0.15/np.asarray(values).std(ddof=1))
# Get KDE value for the point.
iso1 = kernel((x1,y1))
print 'iso1 = ', iso[0]

# -------------------------------------------------------
# Perform a kernel density estimate on the data using sklearn.
kernel_sk = KernelDensity(kernel='gaussian', bandwidth=0.15).fit(zip(*values))
# Get KDE value for the point.
iso2 = kernel_sk.score_samples([[x1, y1]])
print 'iso2 = ', np.exp(iso2[0])

iso2表示为指数,因为sklearn返回日志值)

我得到的iso1iso2的结果是不同的,我失去了如何影响带宽(在任一函数中)以使它们相等(应该如此)。 / p>


添加

我在sklearn聊天(ep)时建议我在使用(x,y)计算内核之前缩放scipy中的值,以便获得与{{1}相当的结果}}

所以这就是我所做的:

sklearn

ie:我在使用# Scale values. x_val_sca = np.asarray(values[0])/np.asarray(values).std(axis=1)[0] y_val_sca = np.asarray(values[1])/np.asarray(values).std(axis=1)[1] values = [x_val_sca, y_val_sca] kernel = stats.gaussian_kde(values, bw_method=bw_value) 获取内核之前缩放了两个维度,同时保留了scipy中获取内核的行。

这给出了更好的结果,但在获得的内核方面仍存在差异:

kernels

其中红点是代码中的sklearn点。可以看出,密度估计的形状仍然存在差异,尽管非常小。也许这是可以实现的最佳目标?

1 个答案:

答案 0 :(得分:4)

几年后,我尝试了这个,并认为我没有重新扩展数据需要它。带宽值确实需要一些扩展:

# For sklearn
bw = 0.15

# For scipy
bw = 0.15/x.std(ddof=1)

对同一点的两个KDE的评估并不完全相同。例如,这是对(x1, y1)点的评估:

iso1 =  0.00984751705005  # Scipy
iso2 =  0.00989788224787  # Sklearn

但我觉得它足够接近。

这是2D情况的MWE和输出,据我所知,看起来几乎完全相同:

enter image description here

import numpy as np
from scipy import stats
from sklearn.neighbors import KernelDensity
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# Generate random data.
n = 1000
m1, m2 = np.random.normal(-3., 3., size=n), np.random.normal(-3., 3., size=n)
# Define limits.
xmin, xmax = min(m1), max(m1)
ymin, ymax = min(m2), max(m2)
ext_range = [xmin, xmax, ymin, ymax]
# Format data.
x, y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([x.ravel(), y.ravel()])
values = np.vstack([m1, m2])

# Define some point to evaluate the KDEs.
x1, y1 = 0.5, 0.5
# Bandwidth value.
bw = 0.15

# -------------------------------------------------------
# Perform a kernel density estimate on the data using scipy.
# **Bandwidth needs to be scaled to match Sklearn results**
kernel = stats.gaussian_kde(
    values, bw_method=bw/np.asarray(values).std(ddof=1))
# Get KDE value for the point.
iso1 = kernel((x1, y1))
print 'iso1 = ', iso1[0]

# -------------------------------------------------------
# Perform a kernel density estimate on the data using sklearn.
kernel_sk = KernelDensity(kernel='gaussian', bandwidth=bw).fit(zip(*values))
# Get KDE value for the point. Use exponential since sklearn returns the
# log values
iso2 = np.exp(kernel_sk.score_samples([[x1, y1]]))
print 'iso2 = ', iso2[0]


# Plot
fig = plt.figure(figsize=(10, 10))
gs = gridspec.GridSpec(1, 2)

# Scipy
plt.subplot(gs[0])
plt.title("Scipy", x=0.5, y=0.92, fontsize=10)
# Evaluate kernel in grid positions.
k_pos = kernel(positions)
kde = np.reshape(k_pos.T, x.shape)
plt.imshow(np.rot90(kde), cmap=plt.cm.YlOrBr, extent=ext_range)
plt.contour(x, y, kde, 5, colors='k', linewidths=0.6)

# Sklearn
plt.subplot(gs[1])
plt.title("Sklearn", x=0.5, y=0.92, fontsize=10)
# Evaluate kernel in grid positions.
k_pos2 = np.exp(kernel_sk.score_samples(zip(*positions)))
kde2 = np.reshape(k_pos2.T, x.shape)
plt.imshow(np.rot90(kde2), cmap=plt.cm.YlOrBr, extent=ext_range)
plt.contour(x, y, kde2, 5, colors='k', linewidths=0.6)

fig.tight_layout()
plt.savefig('KDEs', dpi=300, bbox_inches='tight')