user308827 user308827 - 1 year ago 419
Python Question

Weighted percentile using numpy

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!

Answer Source

Unfortunately, numpy doesn't have built-in weighted functions for everything, but, you can always put something together.

def weight_array(ar, weights):
     zipped = zip(ar, weights)
     weighted = []
     for i in zipped:
         for j in range(i[1]):
             weighted.append(i[0])
     return weighted


np.percentile(weight_array(ar, weights), 25)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download