Skip to content

Instantly share code, notes, and snippets.

@alexandru-dinu
Created February 23, 2022 18:59
Show Gist options
  • Select an option

  • Save alexandru-dinu/4c6133202d8d379066994f5d11446e9d to your computer and use it in GitHub Desktop.

Select an option

Save alexandru-dinu/4c6133202d8d379066994f5d11446e9d to your computer and use it in GitHub Desktop.
Median given a frequency table
def median_freq_table(freq_table: np.ndarray) -> float:
"""
Find median of an array represented as a frequency table [[ val, freq ]].
"""
values = freq_table[:, 0]
freqs = freq_table[:, 1]
# cumulative frequencies
cf = np.cumsum(freqs)
# total number of elements
n = cf[-1]
# get the left and right buckets
# of where the midpoint falls,
# accounting for both evend and odd lengths
l = (n // 2 - 1) < cf
r = (n // 2) < cf
# median is the midpoint value (which falls in the same bucket)
if n % 2 == 1 or (l == r).all():
return values[r][0]
# median is the mean between the mid adjacent buckets
else:
return np.mean(values[l | r][:2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment