code: def weighted_median(d, w):
      
      dtype = [('w', '%s' % w.dtype), ('v', '%s' % d.dtype)]
      d_w = np.array(list(zip(w, d)), dtype=dtype)
      d_w.sort(order='v')
      reordered_w = d_w['w'].cumsum()
      cumsum_threshold = reordered_w[-1] * 1.0 / 2
      median_inx = (reordered_w >= cumsum_threshold).nonzero()[0][0]
      if reordered_w[median_inx] == cumsum_threshold and len(d) - 1 > median_inx:
          return np.sort(d)[median_inx:median_inx + 2].mean()
      return np.sort(d)[median_inx]
docs: A utility function to find a median of d based on w
  
      Parameters
      ----------
      d          : array
                   (n, 1), variable for which median will be found
      w          : array
                   (n, 1), variable on which d's median will be decided
  
      Notes
      -----
      d and w are arranged in the same order
  
      Returns
      -------
      float
          median of d
  
      Examples
      --------
  
      Creating an array including five integers.
      We will get the median of these integers.
  
      >>> d = np.array([5,4,3,1,2])
  
      Creating another array including weight values for the above integers.
      The median of d will be decided with a consideration to these weight
      values.
  
      >>> w = np.array([10, 22, 9, 2, 5])
  
      Applying weighted_median function
  
      >>> weighted_median(d, w)
      4