Numpy: Split arrays

20 August 2020

Code

# Import library
import numpy as np

# Create an array
x = np.array([[1,2,3,4],
             [11,12,13,14],
             [21,22,23,24],
             [31,32,33,34]]
            )
print('x:\n',x)
print("Shape:", x.shape)
x:
 [[ 1  2  3  4]
 [11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]]
Shape: (4, 4)

.

Split with one integer:

Note: Splits array into equal size arrays along the axis. If that is not possible then an Error is raised. Try using np.array_split() listed below in that case.

# Split with one integer
# into equal parts
y0 = np.split(x, 2, axis=0) # integer is '2'
y1 = np.split(x, 2, axis=1) # integer is '2'

# Output
print("Split(x,2, axis=0):")
print(y0)
print(type(y0))
print('y0[0].shape:', y0[0].shape, '\n')

print("Split(x,2, axis=1):")
print(y1)
print(type(y1))
print('y1[0].shape:', y1[0].shape)
Split(x,2, axis=0):
[array([[ 1,  2,  3,  4],
       [11, 12, 13, 14]]), array([[21, 22, 23, 24],
       [31, 32, 33, 34]])]
<class 'list'>
y0[0].shape: (2, 4) 

Split(x,2, axis=1):
[array([[ 1,  2],
       [11, 12],
       [21, 22],
       [31, 32]]), array([[ 3,  4],
       [13, 14],
       [23, 24],
       [33, 34]])]
<class 'list'>
y1[0].shape: (4, 2)

Split with one integer using np.array_split():

Note: Splits array into equal size array if possible, else unequal sizes.

# Using np.array_split()
# Split with one integer
# into equal parts if possible
# else unequal parts. Does not rais an Error
y0 = np.array_split(x, 2, axis=0) # integer is '2'
y1 = np.array_split(x, 2, axis=1) # integer is '2'

# Output
print("Split(x,2, axis=0):")
print(y0)
print(type(y0))
print('y0[0].shape:', y0[0].shape, '\n')

print("Split(x,2, axis=1):")
print(y1)
print(type(y1))
print('y1[0].shape:', y1[0].shape)
Split(x,2, axis=0):
[array([[ 1,  2,  3,  4],
       [11, 12, 13, 14]]), array([[21, 22, 23, 24],
       [31, 32, 33, 34]])]
<class 'list'>
y0[0].shape: (2, 4) 

Split(x,2, axis=1):
[array([[ 1,  2],
       [11, 12],
       [21, 22],
       [31, 32]]), array([[ 3,  4],
       [13, 14],
       [23, 24],
       [33, 34]])]
<class 'list'>
y1[0].shape: (4, 2)

.

Split with 1-D array of sorted integers:

Note: Splits the array at given indices in the 1-D array

# Split with 1-D array of sorted integers

y0 = np.split(x, [1,3], axis=0) 
y1 = np.split(x, [1,3], axis=1) 

# Output
print("Split(x,2, axis=0):")
print(y0)
print(type(y0))
print('y0[0].shape:', y0[0].shape, '\n')

print("Split(x,2, axis=1):")
print(y1)
print(type(y1))
print('y1[0].shape:', y1[0].shape)
Split(x,2, axis=0):
[array([[1, 2, 3, 4]]), array([[11, 12, 13, 14],
       [21, 22, 23, 24]]), array([[31, 32, 33, 34]])]
<class 'list'>
y0[0].shape: (1, 4) 

Split(x,2, axis=1):
[array([[ 1],
       [11],
       [21],
       [31]]), array([[ 2,  3],
       [12, 13],
       [22, 23],
       [32, 33]]), array([[ 4],
       [14],
       [24],
       [34]])]
<class 'list'>
y1[0].shape: (4, 1)

.

Using .hsplit():

Note: Same as .split() with axis=1

# hsplit 
# split column-wise
hsi = np.hsplit(x, 2)
hsa = np.hsplit(x, [1,3])

# Output
print("hsplit (2):\n", hsi)
print('Length:',len(hsi), '\n') 
print("hsplit ([1,3]):\n", hsa)
print('Length:',len(hsa), '\n') 
hsplit (2):
 [array([[ 1,  2],
       [11, 12],
       [21, 22],
       [31, 32]]), array([[ 3,  4],
       [13, 14],
       [23, 24],
       [33, 34]])]
Length: 2 

hsplit ([1,3]):
 [array([[ 1],
       [11],
       [21],
       [31]]), array([[ 2,  3],
       [12, 13],
       [22, 23],
       [32, 33]]), array([[ 4],
       [14],
       [24],
       [34]])]
Length: 3 

.

Using .vsplit():

Note: Same as .split() with axis=0

# vsplit 
# split row-wise
hsi = np.vsplit(x, 2)
hsa = np.vsplit(x, [1,3])

# Output
print("vsplit (2):\n", hsi)
print('Length:',len(hsi), '\n') 
print("vsplit ([1,3]):\n", hsa)
print('Length:',len(hsa), '\n') 
vsplit (2):
 [array([[ 1,  2,  3,  4],
       [11, 12, 13, 14]]), array([[21, 22, 23, 24],
       [31, 32, 33, 34]])]
Length: 2 

vsplit ([1,3]):
 [array([[1, 2, 3, 4]]), array([[11, 12, 13, 14],
       [21, 22, 23, 24]]), array([[31, 32, 33, 34]])]
Length: 3






Any errors in code above?
Please send a message.