使用Numpy stride_tricks获取非重叠的数组块

Cer*_*rin 12 python numpy

我正在尝试使用numpy.lib.stride_tricks.as_strided来迭代数组的非重叠块,但是我无法找到参数的文档,所以我只能得到重叠的块.

例如,我有一个4x5阵列,我希望得到4个2x2块.我很好,右边和底边的额外细胞被排除在外.

到目前为止,我的代码是:

import sys
import numpy as np

a = np.array([
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
])

sz = a.itemsize
h,w = a.shape
bh,bw = 2,2

shape = (h/bh, w/bw, bh, bw)
strides = (w*sz, sz, w*sz, sz)
blocks = np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

print blocks[0][0]
assert blocks[0][0].tolist() == [[1, 2], [6,7]]
print blocks[0][1]
assert blocks[0][1].tolist() == [[3,4], [8,9]]
print blocks[1][0]
assert blocks[1][0].tolist() == [[11, 12], [16, 17]]
Run Code Online (Sandbox Code Playgroud)

生成的块数组的形状似乎是正确的,但最后两个断言失败,可能是因为我的形状或步幅参数不正确.我应该设置哪些值来获得非重叠块?

unu*_*tbu 13

import numpy as np
n=4
m=5
a = np.arange(1,n*m+1).reshape(n,m)
print(a)
# [[ 1  2  3  4  5]
#  [ 6  7  8  9 10]
#  [11 12 13 14 15]
#  [16 17 18 19 20]]
sz = a.itemsize
h,w = a.shape
bh,bw = 2,2
shape = (h/bh, w/bw, bh, bw)
print(shape)
# (2, 2, 2, 2)

strides = sz*np.array([w*bh,bw,w,1])
print(strides)
# [40  8 20  4]

blocks=np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
print(blocks)
# [[[[ 1  2]
#    [ 6  7]]
#   [[ 3  4]
#    [ 8  9]]]
#  [[[11 12]
#    [16 17]]
#   [[13 14]
#    [18 19]]]]
Run Code Online (Sandbox Code Playgroud)

1in a(ie blocks[0,0,0,0])开始,到达2(ie blocks[0,0,0,1])是一个项目.因为(在我的机器上)a.itemsize是4个字节,所以步幅是1*4 = 4.这给了我们最后一个值strides = (10,2,5,1)*a.itemsize = (40,8,20,4).

1再次开始,到达6(即blocks[0,0,1,0]),是5(即w)项目,所以步幅是5*4 = 20.这说明了倒数第二个值strides.

在开始1再次,去的3(即blocks[0,1,0,0]),为2(即bw)的项目了,所以步幅为2*4 = 8,这说明了在第二个值strides.

最后,从1到达11(即blocks[1,0,0,0])到10(即w*bh)项目开始,所以步幅是10*4 = 40.所以strides = (40,8,20,4).


Stu*_*erg 5

以@ unutbu的答案为例,我编写了一个函数来实现任何ND数组的拼贴技巧.请参阅下面的链接到源.

>>> 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]]]]
Run Code Online (Sandbox Code Playgroud)

[ blockwise_view.py] [ test_blockwise_view.py]