The numpy.array_split()
method in Python is used to split an array into multiple sub-arrays of equal size.
In Python, an array is a data structure that is used to store multiple items of the same type together. Arrays are useful when dealing with many values of the same data type. An array needs to explicitly import the array
module for declaration.
The syntax for the split()
method is as follows:
numpy.array_split(array, indices_or_section, axis=0)
Parameter | Description |
---|---|
array |
The input array (required). |
indices_or_section |
The value for the number of splits we want (required). |
axis |
The axis along which the values are appended (optional). |
The return value of numpy.array_split()
is an array that contains the number of splits, as specified when the method is called.
In the example below, we use the numpy.array_split()
method to split an array into two parts.
import numpy as nparray1 = np.array([1, 2, 3, 4])# splitting the array into twonew_array1 = np.array_split(array1, 2)print('Tne newly splitted arrays are: ', new_array1)
In the next example, we use the numpy.array_split()
method to split an array into three parts.
import numpy as nparray1 = np.array([1, 2, 3, 4])# splitting the array into threenew_array1 = np.array_split(array1, 3)print('The newly splitted arrays are:', new_array1)