How to split a 2D array into a list of smaller 2D arrays in python with numpy ?

Published: July 06, 2021

Tags: Python; Numpy;

DMCA.com Protection Status

Example of how to split a 2D array into a list of smaller 2D arrays in python with numpy:

Create a 2d array

Let's create for example a 2d array of size (m,n) = (9,9)

import numpy as np

x = np.arange(81)
x = x.reshape(9,9)

print(x)

returns

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

Split the 2d array into a list of 3 by 3 arrays

To split an array into smaller 2d arrays a straightforward solution is to use numpy.split.

For example let's split first the array along the axis 0:

l = np.array_split(x,3,axis=0)

note that numpy.split returns a list

print(type(l))
print(len(l))

gives

<class 'list'>

and

3

Now just split again the arrays but along the axis 1:

new_l = []
for a in l:
    l = np.array_split(a,3,axis=1)
    new_l += l

then

print(new_l)
print(len(new_l))

returns

[array([[ 0,  1,  2],
             [ 9, 10, 11],
             [18, 19, 20]]), 
             array([[ 3,  4,  5],
             [12, 13, 14],
             [21, 22, 23]]), 
             array([[ 6,  7,  8],
             [15, 16, 17],
             [24, 25, 26]]), 
             array([[27, 28, 29],
             [36, 37, 38],
             [45, 46, 47]]),
             array([[30, 31, 32],
             [39, 40, 41],
             [48, 49, 50]]), 
             array([[33, 34, 35],
             [42, 43, 44],
             [51, 52, 53]]),
             array([[54, 55, 56],
             [63, 64, 65],
             [72, 73, 74]]), 
             array([[57, 58, 59],
             [66, 67, 68],
             [75, 76, 77]]), 
             array([[60, 61, 62],
             [69, 70, 71],
             [78, 79, 80]])]

and

9

Another example using numpy.split

Another example with an array of size (10,9)

import numpy as np

x = np.arange(90)
x = x.reshape(10,9)

print(x)

gives

[[ 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]
 [81 82 83 84 85 86 87 88 89]]

in that case the slit along the axis = 0 :

l = np.array_split(x,3,axis=0)

will returns 3 arrays of size (4,9) (3,9) (3,9) since n = 10 = 4 + 3 + 3:

[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]]), array([[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]]), array([[63, 64, 65, 66, 67, 68, 69, 70, 71],
             [72, 73, 74, 75, 76, 77, 78, 79, 80],
             [81, 82, 83, 84, 85, 86, 87, 88, 89]])]

new_l = []
for a in l:
    l = np.array_split(a,3,axis=1)
    new_l += l

gives

[array([[ 0,  1,  2],
             [ 9, 10, 11],
             [18, 19, 20],
             [27, 28, 29]]), array([[ 3,  4,  5],
             [12, 13, 14],
             [21, 22, 23],
             [30, 31, 32]]), array([[ 6,  7,  8],
             [15, 16, 17],
             [24, 25, 26],
             [33, 34, 35]]), array([[36, 37, 38],
             [45, 46, 47],
             [54, 55, 56]]), array([[39, 40, 41],
             [48, 49, 50],
             [57, 58, 59]]), array([[42, 43, 44],
             [51, 52, 53],
             [60, 61, 62]]), array([[63, 64, 65],
             [72, 73, 74],
             [81, 82, 83]]), array([[66, 67, 68],
             [75, 76, 77],
             [84, 85, 86]]), array([[69, 70, 71],
             [78, 79, 80],
             [87, 88, 89]])]

Note that in this example the list of smaller 2d arrays do not have the same size since it is not possible to decompose a (10,9) array into (3,3) arrays.

References