2

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]
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
srv_77
  • 547
  • 1
  • 8
  • 20
  • And this values could be anything, like 1, 2, 3? For example map [60, 65, 89] to [1, 2, 3]? – Dani Mesejo Oct 15 '21 at 06:12
  • 1
    Couldn't you calculate assuming higher is better and then just do 100-what you calculated. ie. you assume higher numbers are better, and you calculate a value to be in the 75th percentile, 100-75=25. – 3ddavies Oct 15 '21 at 06:13

3 Answers3

0

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)
Mahrkeenerh
  • 1,104
  • 1
  • 9
  • 25
0

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]

Was'
  • 496
  • 4
  • 21
0

I made another algorithm: this one is based on the index of a number in sorted list.

def percentileofscore(data, i):
    index = sorted(set(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]

Was'
  • 496
  • 4
  • 21
  • Thanks! But, what about when there are two or more scores that are equal? – srv_77 Oct 15 '21 at 16:01
  • yeah this will give same values different percentiles, so you can write `sorted(set(data), reverse=True)` so it will give same values same percentiles. (I edited) – Was' Oct 15 '21 at 16:32