4

有没有办法使用具有观察权重的数据来绘制密度?

我有一个观察x向量和一个整数权重向量y,这y1表明我们有多少观察x1。也就是说,密度

   x    y 
   1    2
   2    2
   2    3 

等于 1, 1, 2, 2, 2, 2 ,2(2x1, 5x2) 的密度。据我了解, matplotlib.pyplot.hist(weights=y)在绘制直方图时允许观察权重。是否有任何等效的计算和绘制密度?

我希望包能够做到这一点的原因是我的数据非常大,我正在寻找更有效的替代方案。

或者,我对其他软件包持开放态度。

4

1 回答 1

4

Statsmodels 的 kde 单变量在其拟合函数中接收权重。请参阅以下代码的输出。

import matplotlib.pyplot as plt
import statsmodels.api as sm
import pandas as pd

df = pd.DataFrame({'x':[1.,2.],'weight':[2,4]})
weighted = sm.nonparametric.KDEUnivariate(df.x)
noweight = sm.nonparametric.KDEUnivariate(df.x)
weighted.fit(fft=False, weights=df.weight)
noweight.fit()

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(noweight.support, noweight.density)
ax2.plot(weighted.support, weighted.density)

ax1.set_title('No Weight')
ax2.set_title('Weighted')

输出: 无重量与加权密度

注意:您对阵列创建的时间问题可能无法解决。因为如源代码中所述:

如果 FFT 为 False,则创建一个 'number_of_obs' x 'gridsize' 中间数组

于 2015-11-08T01:04:19.543 回答