Is there a way to use the numpy.percentile function to compute weighted percentile? Or is anyone aware of an alternative python function to compute weighted percentile?
thanks!
Is there a way to use the numpy.percentile function to compute weighted percentile? Or is anyone aware of an alternative python function to compute weighted percentile?
thanks!
This is my function, it give identical behaviour to
With less memory overhead. np.percentile is an O(n) implementation so it's potentially faster for small weights. It has all the edge cases sorted out - it's an exact solution. The interpolation answers above assume linear, when it's a step for most of the case, except when the weight is 1.
Say we have data [1,2,3] with weights [3, 11, 7] and I want the 25% percentile. My ecdf is going to be [3, 10, 21] and I'm looking for the 5th value. The interpolation will see [3,1] and [10, 2] as the matches and interpolate giving 1.28 despite being entirely in the 2nd bin with a value of 2.
here my solution:
it simply calculates the weighted CDF of the data and then it uses to estimate the weighted percentiles.
Completely vectorized numpy solution
Here is the code I'm using. It's not an optimal one (which I'm unable write in
numpy
), but still much faster and more reliable than accepted solutionExamples:
array([ 1. , 3.2, 9. ])
array([ 1. , 3.2, 9. ])