In Python, an array is a data structure used to store multiple items of the same type. Arrays are useful when dealing with many values of the same data type. An array needs to explicitly import the array
module for declaration.
A 2D array is simply an array of arrays.
The numpy.array_split()
method in Python is used to split a 2D array into multiple sub-arrays of equal size.
array_split()
takes the following parameters:
array
(required): Represents the input array.indices_or_section
(required): Represents the number of splits to be returned.axis
(optional): The axis along which the values are appended.To split a 2D array, pass in the array and specify the number of splits you want.
Now, let’s split a 2D array into three sections or indices.
import numpy as nparray1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])# splitting the array into three indexesnew_array1 = np.array_split(array1, 3)print('The arrays splitted into 3 sections are:', new_array1)
numpy
module.array1
.numpy.array_split()
method to split the given array into three sections, and then assign the result to a variable named new_array1
.new_array1
.