Map each list value to its corresponding percentile

后端 未结 9 503
夕颜
夕颜 2020-11-29 00:11

I\'d like to create a function that takes a (sorted) list as its argument and outputs a list containing each element\'s corresponding percentile.

For example,

相关标签:
9条回答
  • 2020-11-29 00:39

    If I understand you correctly, all you want to do, is to define the percentile this element represents in the array, how much of the array is before that element. as in [1, 2, 3, 4, 5] should be [0.0, 0.25, 0.5, 0.75, 1.0]

    I believe such code will be enough:

    def percentileListEdited(List):
        uniqueList = list(set(List))
        increase = 1.0/(len(uniqueList)-1)
        newList = {}
        for index, value in enumerate(uniqueList):
            newList[index] = 0.0 + increase * index
        return [newList[val] for val in List]
    
    0 讨论(0)
  • 2020-11-29 00:43

    Pure numpy version of Kevin's solution

    As Kevin said, optimal solution works in O(n log(n)) time. Here is fast version of his code in numpy, which works almost the same time as stats.rankdata:

    percentiles = numpy.argsort(numpy.argsort(array)) * 100. / (len(array) - 1)
    

    PS. This is one if my favourite tricks in numpy.

    0 讨论(0)
  • 2020-11-29 00:45

    this might look oversimplyfied but what about this:

    def percentile(x):
        pc = float(1)/(len(x)-1)
        return ["%.2f"%(n*pc) for n, i in enumerate(x)]
    

    EDIT:

    def percentile(x):
        unique = set(x)
        mapping = {}
        pc = float(1)/(len(unique)-1)
        for n, i in enumerate(unique):
            mapping[i] = "%.2f"%(n*pc)
        return [mapping.get(el) for el in x]
    
    0 讨论(0)
提交回复
热议问题