Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python
array_split
communitycreator

What is the numpy.array_split() function in NumPy?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Implementing the array_split() function

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.

RELATED TAGS

numpy
python
array_split
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring