What is the numpy.array_split() function in NumPy?
Overview
The array_split() function in numpy is used to split an input array into multiple sub-arrays, as specified by an integer value.
Syntax
numpy.array_split(array, indices_or_sections, axis=0)
Syntax for the array_split() function in NumPy
Parameter value
The array_split() function takes the following parameter values.
array: This is the input array to be split. It is a required parameter.indices_or_sections: This is an integer representation of the number of the section of the array to be split. An error is raised if the number of splits specified is not possible. It is a required parameter.axis: This is the axis along which the split is done. It is an optional parameter.
Return value
The array_split() function returns a list of sub-arrays of the input array.
Example
from numpy import array, array_split# creating the input arrrayfirst_array = array([1, 2, 3, 4, 5, 6, 7, 8, 9])# splitting the input array into 3 sub-arraysmy_array = array_split(first_array, 3)# printing the split arrayprint(my_array)
Explanation
- Line 1: We import the
arrayandarray_splitfrom thenumpymodule. - Line 3: We create an input array,
first_array, using thearray()function. - Line 6: We split the input array into
3sub-arrays using thearray_split()function. The result is assigned to a variable,my_array. - Line 9: We print the new split array,
my_array.