繁体   English   中英

seaborn distplot的权重选项?

[英]weights option for seaborn distplot?

我想在 seaborn distplot 中有一个权重选项,类似于 numpy 直方图中的选项。 如果没有此选项,唯一的替代方法是将权重应用于输入数组,这可能会导致不切实际的大小(和时间)。

您可以通过使用hist_kws参数将权重传递给底层 matplotlib 的直方图函数来提供权重,如下所示:

sns.distplot(..., hist_kws={'weights': your weights array}, ...)

但请注意,权重将仅传递给基础直方图; kde 和distplot的拟合函数都不会受到影响。

正如@vlasisla 在他们的回答中已经提到的,应该通过关键字参数hist_kws提供权重,以便将它们传递给 mathpolotlib 的hist函数。 但是,除非同时禁用kde (内核密度估计)选项,否则这不会产生任何影响。 这段代码实际上会产生预期的效果:

sns.distplot(x, hist_kws={'weights': x_weights}, kde=False)

要理解为什么权重和 kde 都不允许,让我们考虑以下示例,其中x_weights计算为x_weights = np.ones_like(x) / len(x)以便所有 bin 的高度总和为 1:

# generate 1000 samples from a normal distribution
np.random.seed(8362) 
x = np.random.normal(size=1000)

# calculate weights
x_weights = np.ones_like(x) / len(x)

# figure 1 - use weights
sns.distplot(x, hist_kws={'weights': x_weights}, kde=False)
# figure 2 - default plot with kde
sns.distplot(x)

图 1. 使用带有权重的 dist 而不是 KDE图 2. 使用带有默认参数的 dist

在图 1 中,我们提供了带有权重的dist函数,因此在该图中,所有 bin 的高度总和为 1 在图 2 中, dist的默认行为已启用,因此KDE 函数下的区域总和为 1,并且 bins 的高度相应地标准化。 现在很容易看出,在提供权重时绘制 KDE 确实没有多大意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM