15

There are probably better words to describe this question, however what I am trying to do is the opposite of np.percentile(). I have a list of n numbers, and I want to see what percentile of them are smaller than a given value. Right now the way I get this value is by continuously trying different decimals. What I want Numpy to tell me is this:

Given threshold = 0.20 (input), about 99.847781% (output) of the items in list d are below this percentile.

What I do right now to get this number is pretty sketchy:

>>> np.percentile(np.absolute(d), 99.847781) 0.19999962082827874 >>> np.percentile(np.absolute(d), 99.8477816) 0.19999989822334402 >>> np.percentile(np.absolute(d), 99.8477817) 0.19999994445584851 >>> np.percentile(np.absolute(d), 99.8477818) 0.19999999068835939 ... 
2
  • 1
    Are you looking for sum(d < given_value) / len(d)? If you're using python2 you'd have to cast one of the operands to float Commented Jul 30, 2018 at 14:51
  • 5
    This one? docs.scipy.org/doc/scipy/reference/generated/… Commented Jul 30, 2018 at 14:55

3 Answers 3

18

If I'm understanding your question correctly, something like

sum(d < threshold) / len(d) 

should do it.

Edit: I missed the absolute value in the question -

sum(np.abs(d) < threshold) / float(len(d)) 
Sign up to request clarification or add additional context in comments.

3 Comments

Edited your code to be python functional, but it was the right idea! I guess I was forgetting the basics! Thanks.
@cookiedough your edit is not equivalent to what is posted here. If you wanted the absolute value, do sum(np.absolute(d) < 0.2) / float(len(d))
If d is a numpy array, the code I gave is valid python code (assuming you're using python3).
3

Assuming d is a NumPy array, in general, you can do:

(d < threshold).mean() 

And for absolute values specifically:

(np.abs(d) < threshold).mean() 

Comments

1

The other answers are great. But if there's a chance some values in the array could be identical to the threshold (e.g., array of integers), this trick will handle that:

( (d < threshold).mean() + (d <= threshold).mean() ) / 2 

Just averaging using less than and less than or equal to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.