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' 中间数组