如何在 seaborn 因子图中使用加权平均估计器(包括自举)?

How to use a weighted mean estimator in seaborn factor plot (incl bootstrapping)?

我有一个数据框,其中每一行都有一定的权重,需要在平均计算中考虑。我喜欢 seaborn 因子图及其自举的 95% 置信区间,但无法让 seaborn 接受新的加权均值估计器。

这是我想做的一个例子。

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)

来自@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")

来自@fkloosterman the same github thread:一个应该在 seaborn v0.11.0 及更高版本上的解决方案(在 v0.11.2 上确认):

import seaborn as sns, numpy as np
tips = sns.load_dataset("tips")
tips["weight"] = 10 * np.random.rand(len(tips))
tips["tip_and_weight"] = [ v + w*1j for v,w in zip(tips.tip, tips.weight)]
def weighted_mean(x, **kws):
    return np.sum(np.real(x) * np.imag(x)) / np.sum(np.imag(x))

sns.pointplot(x="size", y="tip_and_weight", data=tips, estimator=weighted_mean, orient='v')