Ben*_*mer 3 python statistics numpy scipy
我正在使用scipy stats包来沿轴进行统计,但是我在使用百分位数统计时遇到了麻烦binned_statistic.我已经推广了下面的代码,我试图在一系列x箱中使用x,y值来获取数据集的第10个百分点,并且它失败了.
我当然可以使用函数选项,例如中位数,甚至是使用的numpy标准差np.std.但是,我无法弄清楚如何使用,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
Run Code Online (Sandbox Code Playgroud)
我很茫然,甚至玩过这样的用户定义函数,但没有运气:
def percentile10():
return(np.percentile(y,10))
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
谢谢.
你定义的函数的问题是它根本不需要参数!它需要采用y与您的示例相对应的参数,如下所示:
def percentile10(y):
return(np.percentile(y,10))
Run Code Online (Sandbox Code Playgroud)
您还可以使用lambda函数简洁:
scist.binned_statistic(x, y, statistic=lambda y: np.percentile(y, 10), bins=20,
range=[(0, 5)])[0]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1339 次 |
| 最近记录: |