快速计算numpy / scipy中的矢量化百分位数?

时间:2021-06-02 19:33:05

given an array of values:

给定一组值:

v = np.random.randn(100)

what's the fastest way to compute the percentile of each element in the array? the following is slow:

计算数组中每个元素百分位数的最快方法是什么?以下是缓慢的:

%timeit map(lambda e: scipy.stats.percentileofscore(v, e), v)
100 loops, best of 3: 5.1 ms per loop

1 个解决方案

#1


3  

You could use scipy.stats.rankdata() to achieve the same result:

您可以使用scipy.stats.rankdata()来实现相同的结果:

In [58]: v = np.random.randn(10)

In [59]: print(list(map(lambda e: scipy.stats.percentileofscore(v, e), v)))
[30.0, 40.0, 50.0, 90.0, 20.0, 60.0, 10.0, 70.0, 80.0, 100.0]

In [60]: from scipy.stats import rankdata

In [61]: rankdata(v)*100/len(v)
Out[61]: array([  30.,   40.,   50.,   90.,   20.,   60.,   10.,   70.,   80.,  100.])

#1


3  

You could use scipy.stats.rankdata() to achieve the same result:

您可以使用scipy.stats.rankdata()来实现相同的结果:

In [58]: v = np.random.randn(10)

In [59]: print(list(map(lambda e: scipy.stats.percentileofscore(v, e), v)))
[30.0, 40.0, 50.0, 90.0, 20.0, 60.0, 10.0, 70.0, 80.0, 100.0]

In [60]: from scipy.stats import rankdata

In [61]: rankdata(v)*100/len(v)
Out[61]: array([  30.,   40.,   50.,   90.,   20.,   60.,   10.,   70.,   80.,  100.])