What is NumPy Array Splitting ? | NumPy Tutorial
What is NumPy Array Splitting?
Splitting is a reverse operation of joining. Joining merges sequence of arrays where splitting splits or breaks one array into multiple arrays.NumPy supports two methods termed as split() method and array_split().
method to break a single array into multiple arrays. We will discuss both in this article.
a. split():
syntax:
split(ary , indices_or_sections , axis)
array: ndarray (array we want to split into sub-arrays)
indices_or_sections : integer or 1-D array.
if is an integer N, the array will be divided into N equal arrays along the axis. If such a split is not possible, an error will be raised.
If the 1-D array of sorted integers, the entries indicate where along the axis the array is split. For example: [1,2]would for axis=0, result in
- arry[:1]
- arry[1,2]
- arry[2:]
If the array index exceeds the dimension along the axis, an empty sub-array is returned.
Axis: int or optional (the axis along which splitting will perform default 0)
It returns sub-arrays and if the indices_or_section is an integer value but the split is not possible then ValueError is raised.
Example Code:
#python code to split an array when indices_or_sections is an integer.
import numpy as np
x =np.arange(10)
print(np.split(x,5))
#output:
#[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8, 9])]
#python code to split an array when indices_or_sections is a 1-D array.
import numpy as np
x =np.arange(10)
print(np.split(x,[4,6]))
#output: [array([0, 1, 2, 3]), array([4, 5]), array([6, 7, 8, 9])]
b). array_split():
This function divides a single array into multiple sub-arrays. It accepts the array we want to split, indices_or_sections, and the axis along which splitting will be performed.
Syntax:
array_split(arry , indices_or_sections , axis)
array: ndarray (array we want to split into sub-arrays)
indices_or_sections: int or 1-D
If is an integer N, the array will be divided into N equal arrays along the axis. If such a split is not possible, an error will be raised.If a 1-D array of sorted integers, the entries indicate where along the axis the array is split. For example: [1,2]would for axis=0, result in
- arry[:1]
- arry[1,2]
- arry[2:]
If the array index exceeds the dimension along the axis, an empty sub-array is returned.
Axis: int or optional (the axis along which splitting will be performed default 0)
The difference between the split() and array_split() method is that when the elements are less in the source array for splitting, array_split() worked properly but the split() method would fail...
For Example:
# This code demonstrate array_split() method working
import numpy as np
x =np.arange(11)
print(np.array_split(x,6))
#output: [array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]),
array([8, 9]), array([10])]
# This code demonstrates the split() method works.
import numpy as np
x =np.arange(11)
print(np.split(x,6))
#output:
raise ValueError(
ValueError: array split does not result in an equal division)
# python code to split a 2-D array into two 2-D sub-arrays along the default axis.
import numpy as np
arry =np.array([[3,7,89,12], [56,80,81,13],[1,2,6,4],[12,34,56,78]])
print(np.array_split(arry,2))
#output:[array([[ 3, 7, 89, 12],
[56, 80, 81, 13]]), array([[ 1, 2, 6, 4],
[12, 34, 56, 78]])]
# python code to split a 2-D array into two 2-D sub-arrays along
axis = 1.
import numpy as np
arry =np.array([[3,7,89,12], [56,80,81,13],[1,2,6,4],[12,34,56,78]])
print(np.array_split(arry,2, axis=1))
#output:[array([[ 3, 7],
[56, 80],
[ 1, 2],
[12, 34]]), array([[89, 12],
[81, 13],
[ 6, 4],
[56, 78]])]
Note: similar alternates to vstack() , dstack() are available as
vsplit() , dsplit().