Indices of fixed size sub-matrices of numpy array

后端 未结 3 934
长情又很酷
长情又很酷 2020-12-31 10:59

I am implementing an algorithm which requires me to look at non-overlapping consecutive submatrices within a (strictly two dimensional) numpy array. eg, for the 12 by 12

3条回答
  •  感情败类
    2020-12-31 11:24

    I'm adding this answer to an old question since an edit has bumped this question up. Here's an alternative way to calculate blocks:

    size = 3
    lenr, lenc = int(a.shape[0]/size), int(a.shape[1]/size)
    
    t = a.reshape(lenr,size,lenc,size).transpose(0, 2, 1, 3)
    

    Profiling shows that this is the fastest. Profiling done with python 3.5, and the results from map passed to array() for compatibility, since in 3.5 map returns an iterator.

    reshape/transpose:   643 ns per loop
    reshape/index:       45.8 µs per loop
    Map/split:           10.3 µs per loop
    

    It's interesting that the iterator version of map is faster. In any case, using reshape and transpose is fastest.

提交回复
热议问题