-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use a weighted mean estimator in seaborn factor plot (incl bootstrapping)? #722
Comments
It's not really supported, but I think it is possible to hack together a solution. This seems to work? 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") |
Thanks Michael! It looks like this is working. I really appreciate the hacky but clever workaround. |
mwaskom's solution works when "orient="v" is specified. |
Solution worked for me only when I added: tips["tip_and_weight"] = list(zip(tips.tip, tips.weight)) |
The above solution (which is very useful) works with
MWE: import seaborn as sns, numpy as np
tips = sns.load_dataset("tips")
tips["weight"] = 10 * np.random.rand(len(tips))
tips["tip_and_weight"] = list(zip(tips.tip, tips.weight))
def weighted_mean(x, **kws):
val, weight = map(np.asarray, zip(*x))
return (val * weight).sum() / weight.sum()
sns.pointplot(x="size", y="tip_and_weight", data=tips, estimator=weighted_mean, orient='v') |
Another workaround that does not trigger
|
Oh that’s super clever! |
Will this be a supported option in the future? Not with the hacky workaround? |
[Note: I am reviving a stackoverflow question that I was unable to figure out with some new insights on how it might work. See: http://stackoverflow.com/questions/32771520/how-to-use-a-weighted-mean-estimator-in-seaborn-factor-plot-incl-bootstrapping]
I have a dataframe where each of the rows has a certain weight which needs to be accounted for in the mean computations. I love seaborn factorplots and their bootstrapped 95% confidence intervals but haven't been able to get seaborn to accept a new weighted mean estimator.
Here is an example of what I would like to do.
The problem I have is that the estimator function only gets to see the "main variable" (y axis) instead of the full dataframe that would allow the estimator to access more than just "y".
See here:
seaborn/seaborn/categorical.py
Line 1186 in f4e7777
Is there any simple way to do this?
If not, what is the easiest way to extend the categorical plotting to allow for weighted estimators?
Thanks a lot,
Tim
PS: couldn't figure out labels. my guess is question and wishlist.
The text was updated successfully, but these errors were encountered: