Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
NumPy split()
In this tutorial, you will learn about the numpy.split() method with the help of examples.
The NumPy split() method splits an array into multiple sub-arrays.
Example
import numpy as np
# create a 1-D array
array1 = np.array([0, 1, 2, 3])
# split into two equal length sub-arrays
subArrays= np.split(array1, 2)
print(subArrays)
'''
Output: [array([0, 1]), array([2, 3])]
'''
split() Syntax
The syntax of split() is:
numpy.split(array, indices_or_sections, axis = 0)
split() Arguments
The split() method takes the following arguments:
array- the array to splitindices- defines indices (asintor1-D array) in which the array is splitaxis(optional) - axis along which the array is to be split
Notes:
- If
indicesare an integer(N), the array is divided into N equal parts.- If N equal divisions are not possible, an error is raised.
- If
indicesis a 1-D array, the entries indicate the indices where the input array is divided.
split() Return Value
The split() method returns the list of sub-arrays.
Example 1: Split an Array into Three Arrays
import numpy as np
array1 = np.array( [[1, 2], [3, 4], [5, 6]] )
# split into 3 arrays
splitArrays = np.split(array1, 3)
print(splitArrays)
Output
[array([[1, 2]]), array([[3, 4]]), array([[5, 6]])]
Example 2: Split an Array by Index
import numpy as np
array1 = np.array( [1, 2, 3, 4, 5, 6] )
# indices at which array is split
splitIndices = [2, 5, 8]
# split into subarrays
splitArrays = np.split(array1, splitIndices)
print(splitArrays)
Output
[array([1, 2]), array([3, 4, 5]), array([6]), array([], dtype=int64)]
When an index exceeds the dimension of the array, the function returns an empty subarray.
Example 3: Split an Array Across Different Axes
The third parameter is used to split NumPy arrays across different axes. By default, axis is set to 0 (column-wise).
import numpy as np
# create a 3D array
array1 = np.array([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
# split the array column-wise (axis = 0 by default)
splitArrays = np.split(array1, 2)
print("\nSplit array column-wise:")
print(splitArrays)
# split the array row-wise
splitArrays = np.split(array1, 2, axis = 1)
print("Split array row-wise:")
print(splitArrays)
# split the array depth-wise
splitArrays = np.split(array1, 2, axis = 2)
print("\nSplit array depth-wise:")
print(splitArrays)
Output
Split array column-wise:
[array([[[1, 2],
[3, 4]]]), array([[[5, 6],
[7, 8]]])]
Split array row-wise:
[array([[[1, 2]],
[[5, 6]]]), array([[[3, 4]],
[[7, 8]]])]
Split array depth-wise:
[array([[[1],
[3]],
[[5],
[7]]]), array([[[2],
[4]],
[[6],
[8]]])]
>
Example 4: Split an Array into Uneven Sub-arrays
The split() function cannot split an array into uneven arrays.
import numpy as np
array1 = np.array( [[1, 2], [3, 4], [5, 6]] )
# split into 2 arrays
splitArrays = np.split(array1, 2)
print(splitArrays)
Output
ERROR! raise ValueError( ValueError: array split does not result in an equal division
To split an array into unequal subarrays, you can use array_split() instead. Let's see an example.
import numpy as np
array1 = np.array( [[1, 2], [3, 4], [5, 6]] )
# split into 2 arrays
splitArrays = np.array_split(array1, 2)
for array in splitArrays:
print(array,'\n')
Output
[[1 2] [3 4]] [[5 6]]
Related Methods
We can also split an array along different axes using the following methods:
- NumPy
hsplit()- split an array into multiple sub-arrays horizontally - NumPy
vsplit()- split an array into multiple sub-arrays vertically - NumPy
dsplit()- split an array into multiple sub-arrays along the 3rd axis (depth)
Let's see an example.
import numpy as np
# create a 3D array
array1 = np.array([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
# split the array column-wise
splitArrays = np.vsplit(array1, 2)
print("\nSplit array column-wise:")
for arr in splitArrays:
print(arr)
# split the array row-wise
splitArrays = np.hsplit(array1, 2)
print("Split array row-wise:")
for arr in splitArrays:
print(arr)
# split the array depth-wise
splitArrays = np.dsplit(array1, 2)
print("\nSplit array depth-wise:")
for arr in splitArrays:
print(arr)
Output
Split array column-wise: [[[1 2] [3 4]]] [[[5 6] [7 8]]] Split array row-wise: [[[1 2]] [[5 6]]] [[[3 4]] [[7 8]]] Split array depth-wise: [[[1] [3]] [[5] [7]]] [[[2] [4]] [[6] [8]]]