• JUPYTER
  • FAQ
  • View as Code
  • Python [conda env:py3] Kernel
  • View on Gist
  • Execute on Binder
  • Download Notebook
In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Weighted median¶

In the video we have discussed that for MAPE metric the best constant prediction is weighted median with weights

$$w_i = \frac{\sum_{j=1}^N \frac{1}{x_j}}{x_i}$$

for each object $x_i$.

This notebook exlpains how to compute weighted median. Let's generate some data first, and then find it's weighted median.

In [2]:
N = 5
x = np.random.randint(low=1, high=100, size=N)
x
Out[2]:
array([63, 52, 70, 17, 76])
  1. Compute normalized weights:
In [3]:
inv_x = 1.0/x
inv_x
Out[3]:
array([ 0.01587302,  0.01923077,  0.01428571,  0.05882353,  0.01315789])
In [4]:
w = inv_x/sum(inv_x)
w
Out[4]:
array([ 0.13078104,  0.15844626,  0.11770294,  0.48465916,  0.1084106 ])
  1. Now sort the normalized weights. We will use argsort (and not just sort) since we will need indices later.
In [5]:
idxs = np.argsort(w)
sorted_w = w[idxs]
sorted_w
Out[5]:
array([ 0.1084106 ,  0.11770294,  0.13078104,  0.15844626,  0.48465916])
  1. Compute cumulitive sum of sorted weights
In [6]:
sorted_w_cumsum = np.cumsum(sorted_w)
plt.plot(sorted_w_cumsum); plt.show()
print ('sorted_w_cumsum: ', sorted_w_cumsum)
No description has been provided for this image
sorted_w_cumsum:  [ 0.1084106   0.22611354  0.35689458  0.51534084  1.        ]
  1. Now find the index when cumsum hits 0.5:
In [7]:
idx = np.where(sorted_w_cumsum>0.5)[0][0]
idx
Out[7]:
3
  1. Finally, your answer is sample at that position:
In [8]:
pos = idxs[idx]
x[pos]
Out[8]:
52
In [9]:
print('Data: ', x)
print('Sorted data: ', np.sort(x))
print('Weighted median: %d, Median: %d' %(x[pos], np.median(x)))
Data:  [63 52 70 17 76]
Sorted data:  [17 52 63 70 76]
Weighted median: 52, Median: 63

Thats it!

If the procedure looks surprising for you, try to do steps 2--5 assuming the weights are $w_i=\frac{1}{N}$. That way you will find a simple median (not weighted) of the data.

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by OVHcloud

nbviewer GitHub repository.

nbconvert version: 7.16.6

Rendered (Fri, 01 May 2026 14:32:13 UTC)