Using Numpy stride_tricks to get non-overlapping array blocks

后端 未结 3 1290
执念已碎
执念已碎 2020-11-29 09:39

I\'m trying to using numpy.lib.stride_tricks.as_strided to iterate over non-overlapping blocks of an array, but I\'m having trouble finding documentation of the parameters,

3条回答
  •  再見小時候
    2020-11-29 10:18

    Using @unutbu's answer as an example, I wrote a function that implements this tiling trick for any ND array. See below for link to source.

    >>> a = numpy.arange(1,21).reshape(4,5)
    
    >>> print a
    [[ 1  2  3  4  5]
     [ 6  7  8  9 10]
     [11 12 13 14 15]
     [16 17 18 19 20]]
    
    >>> blocks = blockwise_view(a, blockshape=(2,2), require_aligned_blocks=False)
    
    >>> print blocks
    [[[[ 1 2]
       [ 6 7]]
    
      [[ 3 4]
       [ 8 9]]]
    
    
     [[[11 12]
       [16 17]]
    
      [[13 14]
       [18 19]]]]
    

    [blockwise_view.py] [test_blockwise_view.py]

提交回复
热议问题