I have a 2D array t
in numpy:
>>> t = numpy.array(range(81)).reshape((9,9))
>>> t
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8],
[ 9, 10, 11, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 24, 25, 26],
[27, 28, 29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42, 43, 44],
[45, 46, 47, 48, 49, 50, 51, 52, 53],
[54, 55, 56, 57, 58, 59, 60, 61, 62],
[63, 64, 65, 66, 67, 68, 69, 70, 71],
[72, 73, 74, 75, 76, 77, 78, 79, 80]])
It is indexed by two numbers: row and column index.
>>> t[2,3]
21
>>> t.shape
(9, 9)
>>> t.strides
(72, 8)
What I want to do is to divide the array into rectangular cells of fixed size, 3×3 for example. I'd like to avoid memory copying. The way I try to achieve this is creating a view onto t
with correspondent shape and strides ((3,3,3,3)
and (216,24,72,8)
respectively). This way the first two indexes of the view would mean the position of 3×3 cell in the larger grid and the last two would mean the position of element inside the cell. For example, t[0,1,:,:]
would return
array([[ 3, 4, 5],
[12, 13, 14],
[21, 22, 23]])
So my question is — how to create the described view? Am I missing a simpler method? Can this be done elegantly with slicing syntax?
You can use numpy. split() function to split an array into more than one sub-arrays vertically (row-wise). There are two ways to split the array one is row-wise and the other is column-wise. By default, the array is split in row-wise (axis=0) .
Edit: A way that does not require you to figure out the strides yourself is
numpy.rollaxis(t.reshape(3, 3, 3, 3), 2, 1)
[end of edit]
Another way to achieve this is to use numpy.lib.stride_tricks.as_strided
:
>>> t = numpy.arange(81.).reshape((9,9))
>>> numpy.lib.stride_tricks.as_strided(t, shape=(3,3,3,3), strides=(216,24,72,8))
array([[[[ 0., 1., 2.],
[ 9., 10., 11.],
[ 18., 19., 20.]],
[[ 3., 4., 5.],
[ 12., 13., 14.],
[ 21., 22., 23.]],
[[ 6., 7., 8.],
[ 15., 16., 17.],
[ 24., 25., 26.]]],
[[[ 27., 28., 29.],
[ 36., 37., 38.],
[ 45., 46., 47.]],
[[ 30., 31., 32.],
[ 39., 40., 41.],
[ 48., 49., 50.]],
[[ 33., 34., 35.],
[ 42., 43., 44.],
[ 51., 52., 53.]]],
[[[ 54., 55., 56.],
[ 63., 64., 65.],
[ 72., 73., 74.]],
[[ 57., 58., 59.],
[ 66., 67., 68.],
[ 75., 76., 77.]],
[[ 60., 61., 62.],
[ 69., 70., 71.],
[ 78., 79., 80.]]]])
Note that the strides you provided are correct only for float arrays (itemsize == 8
), while the example t
in your post is an int
array (which might or might no have itemsize == 8
).
You can do:
t = np.arange(81).reshape(9,9)
t.shape = (3, 3, 3, 3)
t = t.transpose((0, 2, 1, 3))
>>> print t.strides
(108, 12, 36, 4)
>>> print t
[[[[ 0 1 2]
[ 9 10 11]
[18 19 20]]
[[ 3 4 5]
[12 13 14]
[21 22 23]]
[[ 6 7 8]
[15 16 17]
[24 25 26]]]
[[[27 28 29]
[36 37 38]
[45 46 47]]
[[30 31 32]
[39 40 41]
[48 49 50]]
[[33 34 35]
[42 43 44]
[51 52 53]]]
[[[54 55 56]
[63 64 65]
[72 73 74]]
[[57 58 59]
[66 67 68]
[75 76 77]]
[[60 61 62]
[69 70 71]
[78 79 80]]]]
transpose will return a view whenever possible, that way you don't have to worry about knowing the data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With