So, I have a list with 25 scores, ranging from 60 to 500
lst= [60, 65, 89, 200, 220, 73, 340, 500.....65]
The lower the score, the better the score. I want to assign each of these scores a percentile that shows which scores are the best/ which are the worst. However, I'm not sure how to do this when the lower score is the better score (versus the higher score being the better score).
I think this would be an example for the opposite of what I'm looking for:
percentileofscore([1, 2, 3, 4], 3)
75.0
percentiles = [percentileofscore(data, i) for i in data]
CodePudding user response:
If your array is sorted (or not), and you want to move by a fixed amount to each item (first gets 0%, second gets 5% (calculated based on length), third gets 10% ...):
def get_score(array, item):
i = sorted(array).index(item)
return i * 100 / (len(array) - 1)
CodePudding user response:
I made this algorithm: lower numbers will have higher percentile and if there are bigger numbers in data, lower numbers get more percentile.
def percentileofscore(data, i):
m = max(data) # highest number
s = m - i # a number that defines the score (higher if i is lower)
return (s*100) / m # making that a percentile
data = [1, 2, 3, 4]
percentiles = [percentileofscore(data, i) for i in data]
percentile
is
[75.0, 50.0, 25.0, 0.0]
Lowest number will not be 100, but highest number will be 0.
more examples
[3, 10, 2]
-> [70.0, 0.0, 80.0]
[2, 3, 4]
-> [50.0, 25.0, 0.0]
[62, 70, 81]
-> [23.45679012345679, 13.580246913580247, 0.0]
CodePudding user response:
I made another algorithm: this one is based on the index of a number in sorted list.
def percentileofscore(data, i):
index = sorted(data, reverse=True).index(i) # index of number in sorted list
return (100 / len(data)) * (index 1) # making that a percentile
data = [1, 2, 3, 4]
percentiles = [percentileofscore(data, i) for i in data]
percentile
is
[100.0, 75.0, 50.0, 25.0]
lowest number will be 100.
more examples
[3, 10, 2]
-> [66.66666666666667, 33.333333333333336, 100.0]
[4, 7, 1, 3, 10]
-> [60.0, 40.0, 100.0, 80.0, 20.0]
[62, 7, 81, 20]
-> [50.0, 100.0, 25.0, 75.0]