NumPy - Split
In this tutorial learn about the followings:
- What does split do in NumPy?
- Types of split method in NumPy with example
What does split do in NumPy?
The numpy.split function in NumPy is a versatile tool used to divide an array into multiple sub-arrays along a specified axis. You provide the array to be divided and either an integer or a 1-D array. If you provide an integer, it defines the number of equal parts to split the array into. If you provide a 1-D array, it contains the indices at which to split the array. An optional argument is axis, which specifies the axis along which to split; the default is 0. If the specified array cannot be split equally according to the input parameters, a ValueError is raised.
Types of split method in NumPy with examples
There are 5 main types of array splitting functions available in NumPy. Here's a brief overview of each type along with examples:
numpy.split: This function splits an array into multiple sub-arrays along a specified axis.numpy.hsplit: This function splits an array into multiple sub-arrays horizontally (column-wise).numpy.vsplit: This function splits an array into multiple sub-arrays vertically (row-wise).numpy.array_split: This function splits an array into multiple sub-arrays. If the array can't be split evenly, it will adjust from the end to make suitable sub-arrays.numpy.dsplit: This function splits an array into multiple sub-arrays along the third axis (depth).
numpy.split
This function splits an array into multiple sub-arrays along a specified axis. The number of equally sized sub-arrays that an array can be divided into is specified. If the array cannot be split equally, it raises a ValueError.
Syntax: numpy.split(ary, indices_or_sections, axis=0)
ary: The array to be divided into sub-arrays.indices_or_sections: If it's an integer, this parameter determines the number of equal arrays to be created from the input array. If it's a 1-D array, it's the indices where the array is split.axis: The axis along which to split. Default is 0.
Here is an example:
import numpy as np
# Define a numpy array
arr = np.array([1, 2, 3, 4, 5, 6])
# Split array into 3 equal sized sub arrays
sub_arrays = np.split(arr, 3)
for sub in sub_arrays:
print(sub)
#output:
# [1 2]
# [3 4]
# [5 6]
numpy.hsplit
The numpy.hsplit function is a tool provided by the NumPy library in Python. This function is used to split an array into multiple smaller arrays, but in a horizontal manner.
Think of it like this: if you have a grid of numbers (like a table with rows and columns), and you want to cut this table vertically into smaller tables (each with the same number of rows but fewer columns), numpy.hsplit is the tool you'd use.
Let's look at an example:
Syntax: numpy.hsplit(ary, indices_or_sections)
Here is an example:
import numpy as np
# Define a 2D numpy array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Horizontally split the array into 3 equal sized sub arrays
sub_arrays = np.hsplit(arr, 3)
for sub in sub_arrays:
print(sub)
#output
#[[1]
# [4]
# [7]]
#[[2]
# [5]
# [8]]
#[[3]
# [6]
# [9]]
numpy.vsplit
This function splits an array into multiple sub-arrays vertically (row-wise). It's equivalent to numpy.split with axis=0 (default), the array is always split along the first axis regardless of the array dimension.
Syntax: numpy.vsplit(ary, indices_or_sections)
Here is an example:
import numpy as np
# Define a 2D numpy array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Before vsplit")
print(arr)
# Vertically split the array into 3 equal sized sub arrays
sub_arrays = np.vsplit(arr, 3)
print("After vsplit")
for sub in sub_arrays:
print(sub)
# output 2D array converted into three 2D arrays row wise
# Before vsplit
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
# After vsplit
# [[1 2 3]]
# [[4 5 6]]
# [[7 8 9]]
numpy.array_split
This function splits an array into multiple sub-arrays along a specified axis. The array can be split into equal parts, but if it's not possible, it will adjust from the end to make suitable sub-arrays, unlike numpy.split.
Syntax:
numpy.array_split(ary, indices_or_sections, axis=0)
Here is an example:
import numpy as np
# Define a numpy array
arr = np.array([1, 2, 3, 4, 5, 6, 7])
# Split array into 3 sub arrays
sub_arrays = np.array_split(arr, 3)
for sub in sub_arrays:
print(sub)
# output
# [1 2 3]
# [4 5]
# [6 7]
numpy.dsplit
The numpy.dsplit function is a tool provided by the NumPy library in Python. It's used to split a 3-dimensional array into multiple smaller 3-dimensional arrays along the depth. Think of it like splitting a cube into smaller cuboids along its depth.
Please note that numpy.dsplit works only with 3D arrays (or higher), as depth doesn't have a meaning in 1D or 2D arrays.
Here's an example:
import numpy as np
# Imagine you have a cube of numbers (2 depth layers, each layer having 2 rows and 3 columns)
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
# You can use numpy.dsplit to split it into 3 smaller cubes, each having 2 layers and each layer having 2 rows and 1 column:
sub_arrays = np.dsplit(arr, 3)
# Now 'sub_arrays' is a list containing 3 smaller arrays.
for sub in sub_arrays:
print(sub)
# output
# [[[ 1]
# [ 4]]
#
# [[ 7]
# [10]]]
# [[[ 2]
# [ 5]]
#
# [[ 8]
# [11]]]
# [[[ 3]
# [ 6]]
#
# [[ 9]
# [12]]]