如何为binned_statistic制作用户定义的函数 [英] How to make user defined functions for binned_statistic
问题描述
我正在使用scipy stats软件包沿轴进行统计,但是在使用binned_statistic
进行百分位数统计时遇到了麻烦.我在下面对代码进行了概括,尝试在一系列x箱中使用具有x,y值的数据集的第10个百分位数,但失败了.
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.
我当然可以使用np.std
进行函数选择,例如中位数,甚至是numpy标准差.但是,我无法弄清楚如何使用np.percentile
,因为它需要2个参数(例如np.percentile(y, 10)
),但随后却给我一个ValueError: statistic not understood
错误.
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.
谢谢.
推荐答案
您定义的函数的问题在于它根本不带任何参数!它需要采用与您的示例相对应的y
参数,如下所示:
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))
为简洁起见,您还可以使用lambda
函数:
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]
这篇关于如何为binned_statistic制作用户定义的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!