Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the percentile of a value

Tags:

python

I have an array of values like [1,2,3,4,5] and I need to find the percentile of each value. The output I am expecting is something like [0,25,50,75,100].

I searched for an API in numpy that could get the desired result and found np.percentile but it does the opposite. Given a percentile value, it will find a value using the input list as the distribution.

Is there an api or way to get this? Thanks

like image 953
Clock Slave Avatar asked May 17 '18 08:05

Clock Slave


2 Answers

To get a value's percentile within a given dataset use scipy's percentileofscore.

from scipy.stats import percentileofscore

dataset = [1,2,3,4,5]

percentile_of_3 = percentileofscore(dataset, 3)
print(percentile_of_3)

[Output] 60.0

This output means that 60% of the values in the dataset are less than or equal to 3. percentileofscore's "kind" argument can be used to specify whether the percentile's cutoff should be inclusive or exclusive. For example:

percentile_of_3 = percentileofscore(dataset, 3)
print(percentile_of_3)

[Output] 40.0

means that 40% of the values in the dataset are less than 3.

If we want a list containing percentiles for each value, we can use list comprehension:

all_percentiles = [percentileofscore(dataset, value, kind='strict') for value in dataset]

[Output] [0.0, 20.0, 40.0, 60.0, 80.0]
like image 76
kidbilly Avatar answered Oct 03 '22 16:10

kidbilly


You should use a list comprehension by dividing each of the list value to the max(lst) -1

lst = [1,2,3,4,5]
max_val = max(lst) -1
lst = [(elem-1)/max_val * 100 for elem in lst]
print(lst)

Output

[0.0, 25.0, 50.0, 75.0, 100.0]

You can also achieve this using numpy arrays.

arr = np.array([1,2,3,4,5])
result = (arr - 1) / (np.max(arr) - 1) * 100
like image 45
Mihai Alexandru-Ionut Avatar answered Oct 03 '22 15:10

Mihai Alexandru-Ionut