Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
numpy
communitycreator

# How to split a 2D array in Numpy

Onyejiaku Theophilus Chidalu

### Overview

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.

### Parameters

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.

### Example

Now, let’s split a 2D array into three sections or indices.

### Code

import numpy as np

array1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])

# splitting the array into three indexes
new_array1 = np.array_split(array1, 3)
print('The arrays splitted into 3 sections are:', new_array1)

Example

### Explanation

• Line 1: We import the numpy module.
• Line 3: We create an array array1.
• Line 5: We use the numpy.array_split() method to split the given array into three sections, and then assign the result to a variable named new_array1.
• Line 6: We print the split 2D array, new_array1.

RELATED TAGS

python
numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time

Copyright ©2022 Educative, Inc. All rights reserved.