Python 如何为binned_统计生成用户定义的函数
我正在使用scipy stats包沿an轴进行统计,但是使用Python 如何为binned_统计生成用户定义的函数,python,numpy,statistics,scipy,Python,Numpy,Statistics,Scipy,我正在使用scipy stats包沿an轴进行统计,但是使用binned_statistic进行百分位统计时遇到了问题。我对下面的代码进行了概括,我尝试在一系列x箱中获取具有x,y值的数据集的第10个百分位,但失败了 当然,我可以使用np.std进行函数选项,比如中值,甚至numpy标准偏差。但是,我不知道如何使用np.percentile,因为它需要两个参数(例如np.percentile(y,10)),但它会给我一个ValueError:statistic not understanding
binned_statistic
进行百分位统计时遇到了问题。我对下面的代码进行了概括,我尝试在一系列x箱中获取具有x,y值的数据集的第10个百分位,但失败了
当然,我可以使用np.std
进行函数选项,比如中值,甚至numpy标准偏差。但是,我不知道如何使用np.percentile
,因为它需要两个参数(例如np.percentile(y,10)
),但它会给我一个ValueError:statistic not understanding
错误
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
我不知所措,甚至玩过这样的用户定义函数,但运气不好:
def percentile10():
return(np.percentile(y,10))
非常感谢您的帮助
谢谢。您定义的函数的问题是它根本不带参数!它需要取一个与您的示例对应的
y
参数,如下所示:
def percentile10(y):
return(np.percentile(y,10))
为了简洁起见,您还可以使用lambda
函数:
scist.binned_statistic(x, y, statistic=lambda y: np.percentile(y, 10), bins=20,
range=[(0, 5)])[0]
谢谢,lambda函数可以工作!我将我的百分位函数错误地转换到我的帖子中,并且在那里有论点,但它仍然不起作用。但是,问题解决了!