如何为 binned_statistic 创建用户定义函数
How to make user defined functions for binned_statistic
我正在使用 scipy 统计数据包沿 an 轴进行统计,但我在使用 binned_statistic
进行百分位统计时遇到了问题。我概括了下面的代码,我在其中尝试使用一系列 x bin 中的 x、y 值获取数据集的第 10 个百分位数,但它失败了。
我当然可以做函数选项,比如中位数,甚至是使用 np.std
的 numpy 标准差。但是,我不知道如何使用 np.percentile
,因为它需要 2 个参数(例如 np.percentile(y, 10)
),但它会给我一个 ValueError: statistic not understood
错误。
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]
我正在使用 scipy 统计数据包沿 an 轴进行统计,但我在使用 binned_statistic
进行百分位统计时遇到了问题。我概括了下面的代码,我在其中尝试使用一系列 x bin 中的 x、y 值获取数据集的第 10 个百分位数,但它失败了。
我当然可以做函数选项,比如中位数,甚至是使用 np.std
的 numpy 标准差。但是,我不知道如何使用 np.percentile
,因为它需要 2 个参数(例如 np.percentile(y, 10)
),但它会给我一个 ValueError: statistic not understood
错误。
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]