Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make user defined functions for binned_statistic

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.

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.

Thanks.

like image 217
Ben O. Avatar asked Aug 22 '15 19:08

Ben O.


1 Answers

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

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]
like image 121
ali_m Avatar answered Sep 19 '22 21:09

ali_m