How to make user defined functions for binned_statistic

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I am using scipy stats package to take statistics along the an axis, but I am having trouble taking the percentile statistic using binned_statistic. I have generalized the code below, where I am trying taking the 10th percentile of a dataset with x, y values within a series of x bins, and it fails.

I can of course do function options, like median, and even the numpy standard deviation using np.std. However, I cannot figure out how to use np.percentile because it requires 2 arguments (e.g. np.percentile(y, 10)), but then it gives me a ValueError: statistic not understood error.

import numpy as np import scipy.stats as scist  y_median = scist.binned_statistic(x,y,statistic='median',bins=20,range=[(0,5)])[0]  y_std = scist.binned_statistic(x,y,statistic=np.std,bins=20,range=[(0,5)])[0]  y_10 = scist.binned_statistic(x,y,statistic=np.percentile(10),bins=20,range=[(0,5)])[0]  print y_median print y_std print y_10 

I am at a loss and have even played around with user defined functions like this, but with no luck:

def percentile10():    return(np.percentile(y,10)) 

Any help, is greatly appreciated.

Thanks.

回答1:

The problem with the function you defined is that it takes no arguments at all! It needs to take a y argument that corresponds to your sample, like this:

def percentile10(y):    return(np.percentile(y,10)) 

You could also use a lambda function for brevity:

scist.binned_statistic(x, y, statistic=lambda y: np.percentile(y, 10), bins=20,                        range=[(0, 5)])[0] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!