Is there a way to make numpy.argmin() as fast as min()?

后端 未结 3 1049
野性不改
野性不改 2020-12-30 00:08

I\'m trying to find the minimum array indices along one dimension of a very large 2D numpy array. I\'m finding that this is very slow (already tried speeding it up with bott

3条回答
  •  [愿得一人]
    2020-12-30 00:27

    In [1]: import numpy as np
    
    In [2]: a = np.random.rand(3000, 16000)
    
    In [3]: %timeit a.min(axis=0)
    1 loops, best of 3: 421 ms per loop
    
    In [4]: %timeit a.argmin(axis=0)
    1 loops, best of 3: 1.95 s per loop
    
    In [5]: %timeit a.min(axis=1)
    1 loops, best of 3: 302 ms per loop
    
    In [6]: %timeit a.argmin(axis=1)
    1 loops, best of 3: 303 ms per loop
    
    In [7]: %timeit a.T.argmin(axis=1)
    1 loops, best of 3: 1.78 s per loop
    
    In [8]: %timeit np.asfortranarray(a).argmin(axis=0)
    1 loops, best of 3: 1.97 s per loop
    
    In [9]: b = np.asfortranarray(a)
    
    In [10]: %timeit b.argmin(axis=0)
    1 loops, best of 3: 329 ms per loop
    

    Maybe min is smart enough to do its job sequentially over the array (hence with cache locality), and argmin is jumping around the array (causing a lot of cache misses)?

    Anyway, if you're willing to keep randvals as a Fortran-ordered array from the start, it'll be faster, though copying into Fortran-ordered doesn't help.

提交回复
热议问题