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
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]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With