# Numpy split array – Python NumPy split() Function

NumPy split() Function:

Numpy split array: The split() function of the NumPy module splits an array into numerous(multiple) sub-arrays as views into the given array.

The split() function is used to build an nd-array from nested lists of splits.

Syntax:

numpy.split(ary, indices_or_sections, axis=0)

Parameters

ary: This is required. It is the input array to be split into multiple sub-arrays.

indices_or_sections: This is required. It specifies the indices or sections as an int or a 1-D array.

• If indices_or_sections is an integer, N, the array is split into N equal arrays along the axis. An error is raised if such a split is not possible.
• If indices_or_sections is a 1-D array of sorted integers, the entries indicate where the array is split along the axis. For example, for axis=0, [2, 3] would yield: ary[:2] ary[2:3] ary[3:]
If an index exceeds the array’s dimension along the axis, an empty sub-array is returned.

axis: This is optional. The axis along which to split, which is set to 0 by default.

Return Value:

Numpy split array: A list of sub-arrays as views into the given ary is returned.

## NumPy split() Function in Python

Example1

The split() function is used to divide(split) an array.

Approach:

• Import numpy module using the import keyword.
• Pass some random list(multi-dimensional) as an argument to the array() function to create an array.
• Store it in a variable.
• Print the above-given array.
• Pass the given array, indices value(here it is 3) as an argument to the split() function to split the given array.
• Store it in another variable.
• Print the above split Array.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(multi-dimensional) as an argument to the array() function to
# create an array.
# Store it in a variable.
gvn_arry = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, indices value(here it is 3) as an argument to the split()function to
# split the given array
# Store it in another variable.
splt_arry = np.split(gvn_arry, 3)
# Print the above split Array
print("The above split Array is:")
print(splt_arry)

Output:

The above given array is:
[[1 2 3]
[4 5 6]
[7 8 9]]
The above split Array is:
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]

Example2

When indices_or_sections is given as a 1-D array of sorted integers, the entries specify where the array is split along the axis. With example, for axis=0, [2, 3] would yield: array[:2], array[2:3], array[3:].

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(multi-dimensional) as an argument to the array() function to
# create an array.
# Store it in a variable.
gvn_arry = np.array([[11, 12, 13, 14],
[15, 16, 17, 18],
[19, 20, 21, 22]])

# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, indices value as 1D array as an argument to the split() to
# split the array
# Store it in another variable.
splt_arry = np.split(gvn_arry, [2,3])
# Print the above split Array
print("The above split Array is:")
print(splt_arry)

Output:

The above given array is:
[[11 12 13 14]
[15 16 17 18]
[19 20 21 22]]
The above split Array is:
[array([[11, 12, 13, 14],
[15, 16, 17, 18]]), array([[19, 20, 21, 22]]), array([], shape=(0, 4), dtype=int64)]