Issue
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
Solution
If your input can contain arbitrary numbers (e. g. [3, 7, 13, 20]
) which are to be mapped to 0% – 100%, then you need to figure out the minimum number and the maximum number and stretch your values to 0 … 100:
values = [ 3, 7, 13, 20 ]
min_value = min(values)
max_value = max(values)
for value in values:
fraction = float(value - min_value) / (max_value - min_value)
percentage = fraction * 100
print(value, percentage)
Or as a comprehension:
percentiles = [ float(value - min_value) / (max_value - min_value) * 100
for value in values ]
This can also be sped up using numpy
for large inputs:
import numpy as np
values = np.array([ 3, 7, 13, 20 ])
min_value = values.min()
max_value = values.max()
percentiles = (values - min_value) / (max_value - min_value) * 100
Answered By - Alfe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.