我有一个数据框,其中每一行都有一定的权重,需要在平均计算中加以考虑。我喜欢seaborn factorplots和他们95%的置信区间,但是无法接受新的加权均值估计。
以下是我想要做的一个例子。
tips_all = sns.load_dataset("tips")
tips_all["weight"] = 10 * np.random.rand(len(tips_all))
sns.factorplot("size", "total_bill",
data=tips_all, kind="point")
# here I would like to have a mean estimator that computes a weighted mean
# the bootstrapped confidence intervals should also use this weighted mean estimator
# something like (tips_all["weight"] * tips_all["total_bill"]).sum() / tips_all["weight"].sum()
# but on bootstrapped samples (for the confidence interval)
答案 0 :(得分:3)
来自@mwaskom:https://github.com/mwaskom/seaborn/issues/722
它并没有真正支持,但我认为可以将解决方案整合在一起。这似乎有用吗?
tips = sns.load_dataset("tips")
tips["weight"] = 10 * np.random.rand(len(tips))
tips["tip_and_weight"] = zip(tips.tip, tips.weight)
def weighted_mean(x, **kws):
val, weight = map(np.asarray, zip(*x))
return (val * weight).sum() / weight.sum()
g = sns.factorplot("size", "tip_and_weight", data=tips,
estimator=weighted_mean, orient="v")
g.set_axis_labels("size", "tip")