We are back with another NumPy article. In this one, we will explore the np.array_split() function.
The array_split() function in NumPy allows us to split an array into sub-arrays of different dimensions.
The syntax of the function is as shown in the code snippet below:
numpy.array_split(ary, indices_or_sections, axis=0)
The function parameters are explained below:
- ary – defines the input array.
- indices_or_sections – determines the number of sections among which the array is split.
- Axis – represents along which axis the collection is divided.
The function will return the array divided into different dimensions.
In the example below, we use the array_split method to split an array into two subarrays.
# import numpy
import numpy as np
arr = np.arange(6).reshape(2,3)
The code above should result in two subarrays as shown:
[array([[0, 1, 2]]), array([[3, 4, 5]])]
We can also perform the same operation on a two-dimensional array as illustrated in the code below:
arr = np.array([[1,2,3], [4,5,6], [7,8,9]])
The above should return:
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
Using this guide, you understood how to use NumPy’s array_split() function to split an array into different dimensions.
Thanks for reading & See you in the next one!!