question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: numpy.percentile output is not sorted

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
arthertzcommented, Dec 9, 2019

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. 20191209_020250

2reactions
A4Visioncommented, Oct 12, 2019

Hi ! Indeed, percentile is elmenet-wise - when considering q, which in our case is np.arange(0, 1, 0.01) * 100. I expect the output to be sorted because q is sorted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.percentile — NumPy v1.24 Manual
Given a vector V of length n , the q-th percentile of V is the value q/100 of the way from the minimum...
Read more >
why percentile() method is not calculating the appropriate ...
1 Answer 1 · Switch to numpy.percentile's default way of calculating quartiles, · provide a value to numpy.percentile's parameter interpolation , ...
Read more >
numpy.percentile — NumPy v1.8 Manual
Compute the qth percentile of the data along the specified axis. ... overwrite_input is True and the input is not already an array,...
Read more >
Percentiles of data set - MATLAB prctile
This MATLAB function returns percentiles of elements in input data A for the percentages p in the interval [0100].
Read more >
Mathematical statistics functions
Median, or 50th percentile, of grouped data. mode() ... Note: The functions do not require the data given to them to be sorted....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found