In this tutorial, we will see how to divide an array into multiple parts. We will use the splitting operation of NumPy arrays to split arrays into rows or columns.
How to split NumPy arrays horizontally into equal parts?
We can use the hsplit()
method of NumPy module to split a given NumPy array into equal parts into columns. Here in the following example, we will implement code to divide an array of size 4×4 into two equal parts of size 4×2.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[B,C] = np.hsplit(A, 2)
print('Array B is:')
print(B)
print(Array C is:)
print(C)
Output:
Array A is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Array B is:
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
Array C is:
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
np.hsplit()
takes the input array to be split as its first argument and number of parts as the second argument. hsplit() method returns a list of NumPy arrays which are the parts of the input array which may be a column or array consisting of many columns.
How to split NumPy arrays vertically into equal parts?
To divide a NumPy array into rows or groups of rows having an equal number of rows, we can use the vsplit() method of NumPy module. As shown in the following example, we can split a 4×4 NumPy array into two arrays of size 2×4.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[B,C] = np.vsplit(A, 2)
print('Array B is:')
print(B)
print(Array C is:)
print(C)
Output:
Array A is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Array B is:
[[0, 1, 2, 3],
[4, 5, 6, 7]]
Array C is:
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]
np.vsplit()
method takes the array to be divided into rows as the first argument and number of parts to be done as a second argument as input. It returns a list of NumPy arrays which consist of rows of input arrays which were passed as arguments to the vsplit()
method.
We can split NumPy arrays non symmetrically using the split()
method of NumPy module. To split an array into non-equal parts, we will have to specify whether we have to split the array horizontally or vertically and indices of the rows or columns from which we have to split the array. np.split()
method takes input array to be split as the first argument and a list of indices from which the array has to be split as the second argument. Axis along which the array has to be split is provided as a third argument. After splitting the array, it returns a list of NumPy arrays as output.
How to split NumPy arrays vertically into non-equal parts?
To split a NumPy array vertically i.e along the columns then we will have to specify the indices of columns by which the array will be split and axis=1
will be provided as an argument to split the array along columns. Following example illustrates the implementation in which we split a 4×4 NumPy array into two arrays of size 4×1 and an array of size 4×2.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[A1,A2,A3] = np.split(A,[1,3],axis=1)
print('Array A1 is:')
print(A1)
print('Array A2 is:')
print(A2)
print('Array A3 is:')
print(A3)
Output:
Array A is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Array A1 is:
[[ 0]
[ 4]
[ 8]
[12]]
Array A2 is:
[[ 1 2]
[ 5 6]
[ 9 10]
[13 14]]
Array A3 is:
[[ 3]
[ 7]
[11]
[15]]
In the above example, index 1 and 3 as passed in a list and the input array is split from the 2nd and 4th column. Here the column index specified in the input will be in the right subarray in output.
How to split NumPy arrays horizontally into non-equal parts?
To split a NumPy array horizontally i.e along the rows then we will have to specify the indices of rows by which the array will be split and axis=0
will be provided as an argument to split the array along rows. Following example illustrates the implementation in which we split a 4×4 NumPy array into two arrays of size 1×4 and an array of size 2×4.
import numpy as np
A = np.arange(16).reshape((4, 4))
print('Array A is:')
print(A)
[A1,A2,A3] = np.split(A,[1,3],axis=0)
print('Array A1 is:')
print(A1)
print('Array A2 is:')
print(A2)
print('Array A3 is:')
print(A3)
Output for the above code snippet is:
Array A is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Array A1 is:
[[0, 1, 2, 3]]
Array A2 is:
[[ 4 5 6 7]
[ 8 9 10 11]]
Array A3 is:
[[12 13 14 15]]
In the above example, index 1 and 3 as passed in a list and the input array is split from the 2nd and 4th row. Here the row index specified in the input will be in the lower sub-array in output.
Conclusion:
In this tutorial, we saw how to split arrays both vertically and horizontally into equal parts. We also looked at methods to split the arrays into non-equal parts horizontally and vertically.
References:
Happy Learning 🙂