Map each list value to its corresponding percentile

后端 未结 9 596
夕颜
夕颜 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:18

    This version allows also to pass exact percentiles values used to ranking:

    def what_pctl_number_of(x, a, pctls=np.arange(1, 101)):
        return np.argmax(np.sign(np.append(np.percentile(x, pctls), np.inf) - a))
    

    So it's possible to find out what's percentile number value falls for provided percentiles:

    _x = np.random.randn(100, 1)
    what_pctl_number_of(_x, 1.6, [25, 50, 75, 100])
    

    Output:

    3
    

    so it hits to 75 ~ 100 range

提交回复
热议问题