BUG: numpy.percentile output is not sorted
See original GitHub issueThe output of numpy.percentile
is not always sorted
Reproducing code example:
import numpy as np
q = np.arange(0, 1, 0.01) * 100
percentile = np.percentile(np.array([0, 1, 1, 2, 2, 3, 3 , 4, 5, 5, 1, 1, 9, 9 ,9, 8, 8, 7]) * 0.1, q)
equals_sorted = np.sort(percentile) == percentile
print(equals_sorted)
assert equals_sorted.all()
Error message:
[ True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True True False False True True True True False True True True False] AssertionError Traceback (most recent call last) <ipython-input-63-2850fe4a4ce3> in <module> 1 q = np.percentile(np.array([0, 1, 1, 2, 2, 3, 3 , 4, 5, 5, 1, 1, 9, 9 ,9, 8, 8, 7]) * 0.1, np.arange(0, 1, 0.01) * 100) 2 equals_sorted = np.sort(q) == q ----> 3 assert equals_sorted.all()
AssertionError:
Numpy/Python version information:
1.17.2 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (10 by maintainers)
Hey, there seems to have been an update to one of the stackexchange answers provided by @eric-wieser with a good alternative interpolation. The thread includes a proof of monotonicity, and the proposed fix appears to address all of the issues mentioned. If this would make sense for the issue, I would be willing to implement this as a first commit, or someone else could try it.
Hi ! Indeed, percentile is elmenet-wise - when considering
q
, which in our case isnp.arange(0, 1, 0.01) * 100
. I expect the output to be sorted becauseq
is sorted.