Map each list value to its corresponding percentile

后端 未结 9 554
夕颜
夕颜 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题