如何为 binned_statistic 制作用户定义的函数 [英] How to make user defined functions for binned_statistic

查看:27
本文介绍了如何为 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: 统计不理解 错误.

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))

任何帮助,不胜感激.

谢谢.

推荐答案

你定义的函数的问题是它根本没有参数!它需要一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆