Ranking of numpy array with possible duplicates

前端 未结 3 2128
你的背包
你的背包 2021-01-17 22:00

I have a numpy array of floats/ints and want to map its elements into their ranks.

If an array doesn\'t have duplicates the problem can be solved by the following co

3条回答
  •  不思量自难忘°
    2021-01-17 22:44

    After upgrading to a latest version of scipy as suggested @WarrenWeckesser in the comments, scipy.stats.rankdata seems to be faster than both scipy.stats.mstats.rankdata and np.searchsorted being the fastet way to do it on larger arrays.

    In [1]: import numpy as np
    
    In [2]: from scipy.stats import rankdata as rd
       ...: from scipy.stats.mstats import rankdata as rd2
       ...: 
    
    In [3]: array = np.arange(0.1, 1000000.1)
    
    In [4]: %timeit np.searchsorted(np.sort(array), array)
    1 loops, best of 3: 385 ms per loop
    
    In [5]: %timeit rd(array)
    10 loops, best of 3: 109 ms per loop
    
    In [6]: %timeit rd2(array)
    1 loops, best of 3: 205 ms per loop
    

提交回复
热议问题