Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compute percentile for an external element with given array?

I’m looking for a percentile function that accepts an array and an element where it would return closest percentile of the element.

Some examples

percentile([1,2,3,4,5], 2) => 40% 
percentile([1,2,3,4,5], 2.5) => 40% 
percentile([1,2,3,4,5], 6) => 100%

Does anything like this or similar exist within python or numpy?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Numpy does this np.percentile(a=[1,2,3,4,5], q=3) => 1.12 which is not desired.

>Solution :

np.percentile(a, q) tells you the qth percentile in the a array. This is the inverse of what you want. I don’t think numpy has a function to do what you want, but it’s easy enough to make your own.

The percentile tells you the percentage of elements of the array that are smaller than the given element, so just do that:

def percentile(lst: list, val) -> float:
    return sum(i <= val for i in lst) / len(lst)

If you have a numpy array, you don’t need to iterate over it since <= will broadcast over the array:

def percentile(arr: np.ndarray, val) -> float:
    return (arr <= val).sum() / len(arr)
>>> percentile([1,2,3,4,5], 2)
# 0.4
>>> percentile([1,2,3,4,5], 2.5)
# 0.4
>>> percentile([1,2,3,4,5], 6)
# 1.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading