In This Article, You Will Learn About Python NumPy Splitting.
Python Numpy Splitting – Before moving ahead, let’s know a little bit about Python NumPy Joining
Splitting is the reverse of joining.
Joining merges multiple arrays in one, and splitting breaks down one array into several.
For splitting arrays, array_split() is used. We pass the array to be split and the number of splits.
Example – Splitting the array into 4 parts.
import numpy as np Array = np.array([11, 12, 13, 14, 15, 16, 17, 18]) updated_Array = np.array_split(Array, 4) print( updated_Array )
Output - [array([11, 12]), array([13, 14]), array([15, 16]), array([17, 18])]
As shown above, it returned a split array into 4 pairs.
Note: If the array contains fewer elements than its requirement, then it adjusts elements from itself at the end.
Example – Splitting the array into 4 parts.
import numpy as np Array = np.array([11, 12, 13, 14, 15, 16, 17]) updated_Array = np.array_split(Array, 4) print(updated_Array)
Output - [array([11, 12]), array([13, 14]), array([15, 16]), array([17])]
As a result, it returned a split array into 4 parts despite founding fewer elements than required to split.
Note: Only split() function is also used for splitting array but it will not adjust array by itself if elements are less than the requirement.
The array_split() return value is an array that contains each split as an array.
You can access an array by dividing it into three arrays, by Index number of arrays.
Example – Accessing the split arrays through array Index number.
import numpy as np Array = np.array([1, 2, 3, 4, 5, 6, 7, 8]) updated_Array = np.array_split(Array, 4) print(updated_Array[0]) print(updated_Array[1]) print(updated_Array[2]) print(updated_Array[3])
As can be seen, it returned arrays that were split according to their index number.
Split 2-D arrays with the same syntax.
Use the array_split() method, pass in the array you want to split and the number of splits you want to do.
Example – Splitting the 2-D array into four 2-D arrays.
import numpy as np Array = np.array([[11, 12], [13, 14], [15, 16], [17, 18], [20, 21], [22, 23], [24, 25], [26, 27]]) updated_Array = np.array_split(Array, 4) print(updated_Array)
Output - [array([[11, 12], [13, 14]]), array([[15, 16], [17, 18]]), array([[20, 21], [22, 23]]), array([[24, 25], [26, 27]])]
As has been noticed, it returned 2-D arrays splitting into four pairs of 2-D arrays.
Let’s take another example. This time, each element in the 2-D arrays has 3 elements.
Example – Splitting the 2-D array into two pairs of 2-D arrays.
import numpy as np Array = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22]]) updated_Array = np.array_split(Array, 2) print(updated_Array)
Output - [array([[11, 12, 13], [14, 15, 16]]), array([[17, 18, 19], [20, 21, 22]])]
Henceforth, it returned 2-D arrays splitting into 2 pairs of 2-D arrays each containing 3 elements.
You can also specify the axis that you would like to split.
The following example also returns two pairs of 2-D arrays. However, they are divided along the row (axis=1).
Example – Splitting the 2-D array into two pairs of 2-D arrays along rows.
import numpy as np Array = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22]]) updated_Array = np.array_split(Array, 2, axis=1) print(updated_Array)
Output - [array([[11, 12], [14, 15], [17, 18], [20, 21]]), array([[13], [16], [19], [22]])]
As a result, it returned 2-D arrays splitting into 2 pairs of 2-D arrays along rows.
Using another solution that’s hsplit() that’s opposite of hstack().
Example – Using the hsplit() method to split the 2-D array into three pairs of arrays along rows.
import numpy as np Array = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22]]) updated_Array = np.hsplit(Array, 3) print(updated_Array)
Output - [array([[11], [14], [17], [20]]), array([[12], [15], [18], [21]]), array([[13], [16], [19], [22]])]
As shown above, it returned split arrays into three pairs of arrays along rows.
If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.
Like us on