How to split an array into multiple arrays in Numpy? Use Python NumPy array split()
function to split an array into more than one (multiple) sub arrays as views. This function divides the array into subarrays along with a specified axis. The function takes three parameters array
, indices_or_sections
, and axis
.
In this article, I will explain with examples how to split Python NumPy array by using numpy.split()
function.
1. Quick Examples of Python NumPy Array Split Function
If you are in a hurry, below are some quick examples of how to use Python NumPy array split() function.
# Below are a quick example
# Example 1: Use numpy.split() function
arr2 = np.split(arr,4)
# Example 2: Use numpy.split() function to split 1-D numpy array
arr2 = np.split(arr,[2,3])
# Example 3: Use split 2-D numpy array use numpy.split() function
arr = np.array([[15,28,57,65],[25,37,55,88]])
arr2 = np.split(arr, 2, axis=0)
# Example 4: Use split array along axis=1
arr2 = np.split(arr, 2, axis=1)
# Example 5: Use numpy.split() function to slicing
arr2 = np.split(arr, (2,3), axis=1)
2. Python NumPy split() Syntax
Following is the syntax of the numpy.split()
function.
# Python numpy.split() syntax
numpy.split(arr, indices_or_sections, axis=0)
2.1 Parameters of split()
Following are the parameters of split()
function.
arr
– Array to be divided into sub-arrays.indices_or_sections
– The parameter can be an integer value or 1-D sorted Numpy integer array. indicating the number of equal-sized subarrays to be created from the input array. If this parameter is a 1-D array, the entries indicate the points at which a new subarray is to be created.axis
– To specify the axis along which to perform the split. By default, axis=0.
2.2 Return Value of split()
It returns a list of sub-arrays as views into arr. If indices_or_sections is given as an integer, but its unable to split in equal division, it raises a ValueError.
3. Use numpy.split() Function
You can split the NumPy array as many parts as you want using the np.split()
function. Let’s say you want to split the array into 4 Parts, so pass the value 4 as an argument to indices_or_sections
param of the split()
function.
import numpy as np
# Creating an input array
arr = np.array([5,7,9,11,13,19,23,27])
# Use numpy.split() function
arr2 = np.split(arr,4)
print(arr2)
# OutPut:
# [array([5, 7]), array([ 9, 11]), array([13, 19]), array([23, 27])]
You can access the element of the split array by using its index arr2[3]
. This returns the 4the element of the array.
4. Use split() Function to Split 1-D Array
To split the array at positions indicated in the 1-Dimensional NumPy array.
# Use numpy.split() function to split 1-D numpy array
arr2 = np.split(arr,[2,3])
print(arr2)
# OutPut:
# [array([5, 7]), array([9]), array([11, 13, 19, 23, 27])]
5. Split 2-D Array Use split() Function
You can use numpy.split()
function to split an array into more than one sub-arrays vertically (row-wise). There are two ways to split the array one is row-wise and the other is column-wise. By default, the array is split in row-wise (axis=0)
.
import numpy as np
# Creating an 2D input array
arr = np.array([[15,28,57,65],[25,37,55,88]])
# Use split array along axis = 0
arr2 = np.split(arr, 2, axis=0)
print(arr2)
# OutPut:
# [array([[15, 28, 57, 65]]), array([[25, 37, 55, 88]])]
You can also use numpy.split()
function to split an array into multiple sub-arrays horizontally (column-wise). You can perform a horizontal split with the numpy.split()
function. By using axis=1
along with the input array and the number of sections to split.
# Use split array along axis=1
arr2 = np.split(arr, 2, axis=1)
print(arr2)
# OutPut:
# [array([[15, 28],
# [25, 37]]), array([[57, 65],
# [55, 88]])]
To split arr
by columns via slicing.
# Use numpy.split() function to slicing
arr2 = np.split(arr, (2,3), axis=1)
print(arr2)
# OutPut:
# [array([[15, 28],
# [25, 37]]), array([[57],
# [55]]), array([[65],
# [88]])]
6. Split() Returning ValueError
If split() function is unable to split in an equal division it returns a ValueError: array split does not result in an equal division. In our example below, I am trying to split 8 elements by 5 slices which is not possible hence it returns an error.
# Creating an input array
arr = np.array([5,7,9,11,13,19,23,27])
# Use numpy.split() function
arr2 = np.split(arr,5)
print(arr2)
# Output:
# ValueError: array split does not result in an equal division
7. Conclusion
In this article, I have explained how to use NumPy array split()
function to split an array into multiple sub-arrays as views into an array with examples.
Happy Learning!!
Related Articles
- How to Calculate minimum() of Array in NumPy
- How to Use NumPy Random choice() in Python
- How to Use NumPy random.normal() In Python
- How to get square values of an array
- Python NumPy repeat() Function
- How to Calculate Maximum of Array in NumPy
- Python NumPy Reverse Array
- NumPy power() Function in Python
- How to Calculate minimum() of Array in NumPy?