Skip to content

Instantly share code, notes, and snippets.

@pkaf
Created October 30, 2015 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkaf/547a3ac5a38b9f3cffd6 to your computer and use it in GitHub Desktop.
Save pkaf/547a3ac5a38b9f3cffd6 to your computer and use it in GitHub Desktop.
Weighted Median
import numpy as np
def weighted_median(x, weights):
#Sorting item and weights first
sort = np.argsort(x)
x = x[sort]
weights = weights[sort]
#Less than cumulative frequencies
cumfreq = np.cumsum(weights)
N = np.sum(weights)
assert N == cumfreq[-1]
#Find minimum cumulative frequency >= N/2.
#If equal, average of x corresponding to min and second min is return
#otherwise x corresponding to min is reported
idx = np.where(cumfreq>= N/2.)[0][0]
if cumfreq[idx]==N/2.:
Md = 0.5*(x[idx] + x[idx+1])
else:
Md = x[idx]
return Md
if __name__=="__main__":
x = np.array([19,28,27,32,37,41,42,24])
freq = np.array([7,12,22,14,13,14,19,19])
#x = np.array([19,28,27,32,37,41,42,24])
#freq = np.array([7,12,25,14,13,14,19,19])
#x = np.array([155,160,165,170.,175,180])
#freq = np.array([4,7,18,11,6,4.])
#freq = np.array([1]*8)
print weighted_median( x, freq)
print np.median(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment