What is the array split() method in Numpy?

Overview

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.

Syntax

The syntax for the split() method is as follows:

numpy.array_split(array, indices_or_section, axis=0)

Parameters

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).

Return value

The return value of numpy.array_split() is an array that contains the number of splits, as specified when the method is called.

Example 1

In the example below, we use the numpy.array_split() method to split an array into two parts.

Code

import numpy as np
array1 = np.array([1, 2, 3, 4])
# splitting the array into two
new_array1 = np.array_split(array1, 2)
print('Tne newly splitted arrays are: ', new_array1)

Example 2

In the next example, we use the numpy.array_split() method to split an array into three parts.

Code

import numpy as np
array1 = np.array([1, 2, 3, 4])
# splitting the array into three
new_array1 = np.array_split(array1, 3)
print('The newly splitted arrays are:', new_array1)

Free Resources