Search code examples
pythonnumpynanmedianpercentile

np.percentile returns different median from np.median when inf is present


When inf values are present in an array, under certain conditions np.percentile can return NaN as the median, whereas np.median can return a finite value.

>>> import numpy as np
>>> np.percentile([np.inf, 5, 4], [10, 20, 30, 40, 50, 60, 70, 80, 90])
/Users/tom/miniconda3/envs/alldev/lib/python3.7/site-packages/numpy-1.16.0.dev0+45718fd-py3.7-macosx-10.7-x86_64.egg/numpy/lib/function_base.py:3947: RuntimeWarning: invalid value encountered in multiply
  x2 = take(ap, indices_above, axis=axis) * weights_above
array([4.2, 4.4, 4.6, 4.8, nan, inf, inf, inf, inf])
>>> np.median([np.inf, 5, 4])
5.0

In this case, np.median is able to correctly return 5.0 as the median value, whereas np.percentile returns NaN for the 50th percentile.


Solution

  • the first parameter is the data, the second parameter is the confidence intervals. you not allowed to put an non number in the confidence interval

    data=[10, 20, 30, 40, 50, 60, 70, 80, 90]
    confidence=[5, 4]
    results=np.percentile(data,confidence )
    
    print(results)
    

    output

     array([14. , 13.2])
    

    values 13.2 through 14 will yield a 4 to 5 percent confidence interval