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!
I don' know what's Weighted percentile means, but from @Joan Smith's answer, It seems that you just need to repeat every element in
ar
, you can usenumpy.repeat()
:the result is:
I use this function for my needs:
Multiply results by 100 if you want percentiles instead of quantiles.
Apologies for the additional (unoriginal) answer (not enough rep to comment on @nayyarv's). His solution worked for me (ie. it replicates the default behavior of
np.percentage
), but I think you can eliminate the for loop with clues from how the originalnp.percentage
is written.A quick solution, by first sorting and then interpolating:
Unfortunately, numpy doesn't have built-in weighted functions for everything, but, you can always put something together.
As mentioned in comments, simply repeating values is impossible for float weights, and impractical for very large datasets. There is a library that does weighted percentiles here: http://kochanski.org/gpk/code/speechresearch/gmisclib/gmisclib.weighted_percentile-module.html It worked for me.