NumPy - Splitting Arrays
Splitting NumPy Array
Splitting arrays in NumPy is a way to divide a single array into multiple sub-arrays. This can be done along any axis, depending on how you want to partition the data. NumPy provides several functions to split arrays in different ways. They are as follows −
- Using numpy.split() Function
- Using numpy.array_split() Function
- Using numpy.hsplit() Function
- Using numpy.vsplit() Function
- Using numpy.dsplit() Function
Splitting Arrays Using split() Function
We can use the split() function in NumPy to split an array into multiple sub-arrays along a specified axis. The array is divided based on the provided indices. Following is the syntax −
numpy.split(array, indices_or_sections, axis=0)
Where,
-
array − The input array to be split.
-
indices_or_sections − This can be either an integer or a 1D array of sorted integers.
If an integer, it specifies the number of equal-sized sub-arrays to create. The array must be divisible evenly into this number of sections.
If a 1D array of sorted integers, it specifies the points at which to split the array.
-
axis − The axis along which to split the array. The default is 0 (split along rows for 2D arrays).
Example: Splitting into Equal-sized Sub-arrays
In the below example, we are splitting an array “arr” into 3 equal sub-arrays along the columns (axis=1) using the numpy.split() function−
import numpy as np
# array
arr = np.arange(9).reshape(3, 3)
# Split into 3 equal sub-arrays
split_arr = np.split(arr, 3, axis=1)
print("Original Array:")
print(arr)
print("\nSplit into 3 equal sub-arrays along axis 1:")
for sub_arr in split_arr:
print(sub_arr)
Following is the output obtained −
Original Array:
[[0 1 2]
[3 4 5]
[6 7 8]]
Split into 3 equal sub-arrays along axis 1:
[[0]
[3]
[6]]
[[1]
[4]
[7]]
[[2]
[5]
[8]]
Example: Splitting at Specific Indices
Here, we are splitting an array at indices [1, 2] along the rows (axis=0) using the split() function in NumPy −
import numpy as np
# array
arr = np.arange(9).reshape(3, 3)
# Split array at specified indices
split_arr = np.split(arr, [1, 2], axis=0)
print("\nSplit at indices [1, 2] along axis 0:")
for sub_arr in split_arr:
print(sub_arr)
This will produce the following result −
Split at indices [1, 2] along axis 0:
[[0 1 2]]
[[3 4 5]]
[[6 7 8]]
Splitting Arrays Using array_split() Function
We can also use the array_split() function in NumPy to split an array into multiple sub-arrays along a specified axis. Unlike numpy.split() function, the array_split() function allows for unequal splits if the array cannot be evenly divided.
When the array does not evenly divide into the specified number of sections, the numpy.array_split() function ensures that the resulting sub-arrays are as equal in size as possible, distributing any extra elements to the earlier sub-arrays. Following is the syntax −
numpy.array_split(array, indices_or_sections, axis=0)
Where,
-
array − The input array to be split.
-
indices_or_sections − This can be either an integer or a 1D array of sorted integers.
If an integer, it specifies the number of equal-sized sub-arrays to create. The array must be divided as equally as possible.
If a 1D array of sorted integers, it specifies the points at which to split the array.
-
axis − The axis along which to split the array. The default is 0 (split along rows for 2D arrays).
Example
In the example below, we are splitting a 1D array with “10” elements into 3 unequal sub-arrays using numpy.array_split() function −
import numpy as np
# array
arr = np.arange(10)
# Split into 3 sub-arrays along axis 0
split_arr = np.array_split(arr, 3)
print("Original Array:")
print(arr)
print("\nSplit into 3 unequal sub-arrays:")
for sub_arr in split_arr:
print(sub_arr)
Following is the output of the above code −
Original Array:
[0 1 2 3 4 5 6 7 8 9]
Split into 3 unequal sub-arrays:
[0 1 2 3]
[4 5 6]
[7 8 9]
Horizontal Splitting
We can split an array along the horizontal axis, which is axis = 1, for 2D arrays using the hsplit() function in NumPy. This function divides the array into sub-arrays horizontally, effectively separating columns of data. Following is the syntax −
numpy.hsplit(array, indices_or_sections)
Where,
- array − The input array to be split.
- indices_or_sections − Either an integer or a 1D array of indices that indicate how to split the array.
Example
In this example, we are splitting a 2D array “arr” along its columns into 2 equal parts using the numpy.hsplit() function −
import numpy as np
# 2D array
arr = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
# Split into 2 equal parts along axis 1
split_arr = np.hsplit(arr, 2)
print("Original Array:")
print(arr)
print("\nSplit into 2 equal parts along axis 1:")
for sub_arr in split_arr:
print(sub_arr)
After executing the above code, we get the following output −
Original Array:[[1 2 3 4]
[5 6 7 8]]
Split into 2 equal parts along axis 1:
[[1 2]
[5 6]]
[[3 4]
[7 8]]
Vertical Splitting
We can also split an array along the vertical axis, which is axis = 0, for 2D arrays using the vsplit() function in NumPy. This function divides the array into sub-arrays vertically, effectively separating rows of data. Following is the syntax −
numpy.vsplit(array, indices_or_sections)
Where,
- array − The input array to be split.
- indices_or_sections − Either an integer or a 1D array of indices that indicate how to split the array.
Example
In the example below, we are splitting a 2D array “arr” along its rows into 3 equal parts using the numpy.vsplit() function −
import numpy as np
# 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Split into 3 equal parts along axis 0
split_arr = np.vsplit(arr, 3)
print("Original Array:")
print(arr)
print("\nSplit into 3 equal parts along axis 0:")
for sub_arr in split_arr:
print(sub_arr)
The output obtained is as shown below −
Original Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Split into 3 equal parts along axis 0:
[[1 2 3]]
[[4 5 6]]
[[7 8 9]]
Depth Splitting
The numpy.dsplit() function is used to split a 3D array along its third dimension. This dimension is commonly referred to as the depth dimension, corresponding to axis=2. Following is the syntax −
numpy.dsplit(array, indices_or_sections)
Example
In this example, using the numpy.dsplit() function to split a 3D array “arr” into four equal parts along its third dimension −
import numpy as np
# Example 3D array
arr = np.arange(24).reshape((2, 3, 4))
# Split into 4 equal parts along axis 2 (depth)
split_arr = np.dsplit(arr, 4)
print("Original Array:")
print(arr)
print("\nSplit into 4 equal parts along axis 2 (depth):")
for sub_arr in split_arr:
print(sub_arr)
print()
The result produced is as follows −
Original 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]]]
Split into 4 equal parts along axis 2 (depth):
[[[ 0]
[ 4]
[ 8]]
[[12]
[16]
[20]]]
[[[ 1]
[ 5]
[ 9]]
[[13]
[17]
[21]]]
[[[ 2]
[ 6]
[10]]
[[14]
[18]
[22]]]
[[[ 3]
[ 7]
[11]]
[[15]
[19]
[23]]]