使用Numpy stride_tricks获取不重叠的数组块 [英] Using Numpy stride_tricks to get non-overlapping array blocks

查看:131
本文介绍了使用Numpy stride_tricks获取不重叠的数组块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy.lib.stride_tricks.as_strided遍历数组的非重叠块,但是在查找参数文档时遇到了麻烦,因此我只能获取重叠块

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, so I've only been able to get overlapping blocks.

例如,我有一个4x5数组,我想从中获取4个2x2块.我可以排除右侧和底部边缘的多余单元格.

For example, I have a 4x5 array which I'd like to get 4 2x2 blocks from. I'm fine with the extra cells on the right and bottom edge being excluded.

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

So far, my code is:

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]]

生成的块数组的形状似乎是正确的,但是最后两个断言失败了,大概是因为我的shape或stride参数不正确.我应该为这些值设置哪些值以获取不重叠的块?

The shape of the resulting blocks array seems to be correct, but the last two asserts fail, presumably because my shape or strides parameters are incorrect. What values for these should I set to get non-overlapping blocks?

推荐答案

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]]]]

a(即blocks[0,0,0,0])中的1开始,到2(即blocks[0,0,0,1]),只有一小段距离.由于(在我的机器上)a.itemsize是4个字节,因此步幅为1 * 4 =4.这为我们提供了strides = (10,2,5,1)*a.itemsize = (40,8,20,4)中的最后一个值.

Starting at the 1 in a (i.e. blocks[0,0,0,0]), to get to the 2 (i.e. blocks[0,0,0,1]) is one item away. Since (on my machine) the a.itemsize is 4 bytes, the stride is 1*4 = 4. This gives us the last value in strides = (10,2,5,1)*a.itemsize = (40,8,20,4).

再次从1开始,到达6(即blocks[0,0,1,0]),相距5(即w)个项目,因此跨度为5 * 4 = 20. strides中的倒数第二个值.

Starting at the 1 again, to get to the 6 (i.e. blocks[0,0,1,0]), is 5 (i.e. w) items away, so the stride is 5*4 = 20. This accounts for the second to last value in strides.

再次从1开始,要到达3(即blocks[0,1,0,0]),则要有2(即bw)个项目,所以步幅为2 * 4 = 8. strides中的第二个值.

Starting at the 1 yet again, to get to the 3 (i.e. blocks[0,1,0,0]), is 2 (i.e. bw) items away, so the stride is 2*4 = 8. This accounts for the second value in strides.

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

Finally, starting at the 1, to get to 11 (i.e. blocks[1,0,0,0]), is 10 (i.e. w*bh) items away, so the stride is 10*4 = 40. So strides = (40,8,20,4).

这篇关于使用Numpy stride_tricks获取不重叠的数组块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆