Creating a 4D view on a 2D array for dividing it into fixed-size cells

I have a two dimensional 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: the row and column index.

 >>> t[2,3] 21 >>> t.shape (9, 9) >>> t.strides (72, 8) 

What I want to do is split the array into rectangular cells of a fixed size , for example 3 × 3. I would like to avoid copying the memory. The way I'm trying to do this is to create a view on t with the appropriate form and steps ( (3,3,3,3) and (216,24,72,8) respectively). Thus, the first two view indices will indicate the position of a 3 × 3 cell in the larger grid, and the last two will indicate the position of an element within the cell. For example, t[0,1,:,:] will 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 slice syntax?

+7
source share
2 answers

Change A method that does not require you to figure out what this step is:

 numpy.rollaxis(t.reshape(3, 3, 3, 3), 2, 1) 

[end of editing]

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

Please note that the steps you provided are only valid for floating point arrays ( itemsize == 8 ), and the example t in your message is an int array (which may or may not have itemsize == 8 ).

+6
source

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 the view whenever possible, so you don’t have to worry about knowing the data type.

+2
source

All Articles