Fast numpy fancy indexing

前端 未结 4 1472
别跟我提以往
别跟我提以往 2020-12-14 04:40

My code for slicing a numpy array (via fancy indexing) is very slow. It is currently a bottleneck in program.

a.shape
(3218, 6)

ts = time.time(); a[rows][:,         


        
4条回答
  •  没有蜡笔的小新
    2020-12-14 05:13

    You can get some speed up if you slice using fancy indexing and broadcasting:

    from __future__ import division
    import numpy as np
    
    def slice_1(a, rs, cs) :
        return a[rs][:, cs]
    
    def slice_2(a, rs, cs) :
        return a[rs[:, None], cs]
    
    >>> rows, cols = 3218, 6
    >>> rs = np.unique(np.random.randint(0, rows, size=(rows//2,)))
    >>> cs = np.unique(np.random.randint(0, cols, size=(cols//2,)))
    >>> a = np.random.rand(rows, cols)
    >>> import timeit
    >>> print timeit.timeit('slice_1(a, rs, cs)',
                            'from __main__ import slice_1, a, rs, cs',
                            number=1000)
    0.24083110865
    >>> print timeit.timeit('slice_2(a, rs, cs)',
                            'from __main__ import slice_2, a, rs, cs',
                            number=1000)
    0.206566124519
    

    If you think in term of percentages, doing something 15% faster is always good, but in my system, for the size of your array, this is taking 40 us less to do the slicing, and it is hard to believe that an operation taking 240 us will be your bottleneck.

提交回复
热议问题