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 arrray
first_array = array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# splitting the input array into 3 sub-arrays
my_array = array_split(first_array, 3)
# printing the split array
print(my_array)

Explanation

  • Line 1: We import the array and array_split from the numpy module.
  • Line 3: We create an input array, first_array, using the array() function.
  • Line 6: We split the input array into 3 sub-arrays using the array_split() function. The result is assigned to a variable, my_array.
  • Line 9: We print the new split array, my_array.

Free Resources