LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science NumPy Splitting Array
Splitting NumPy Arrays: Splitting is reverse operation of Joining.
Joining merges multiple arrays into one and Splitting breaks one array into multiple.
We use array_split() for splitting arrays, we pass it to the array we want to split and the number of splits.
Example 1: Split the array in 3 parts.
Code
import numpy as np
arr = np.array([11, 12, 13, 14, 15, 16])
newarr = np.array_split(arr, 3)
print(newarr)
the output will be
[array([11, 12]), array([13, 14]), array([15, 16])]
Note: The return value is an array containing three arrays.
If the array has less elements than required, it will adjust from the end accordingly.
Example 2: Split the array in 4 parts.
Code
import numpy as np
arr = np.array([11, 12, 13, 14, 15, 16])
newarr = np.array_split(arr, 4)
print(newarr)
the output will be
[array([11, 12]), array([13, 14]), array([15]), array([16])]
Note: We also have the method split() available but it will not adjust the elements when elements are less in source array for splitting like
in example above, array_split() worked properly but split() would fail.
Split Into Arrays
The return value of the array_split() method is an array containing each of the split as an array.
If you split an array into 3 arrays, you can access them from the result just like any array element.
Example 3: Access the splitted arrays.
Code
import numpy as np
arr = np.array([11, 12, 13, 14, 15, 16])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
the output will be
[11 12]
[13 14]
[15 16]
Splitting 2-D Arrays
Use the same syntax when splitting 2-D arrays.
Use the array_split() method, pass in the array you want to split and the number of splits you want to do.
Example 4: Split the 2-D array into three 2-D arrays.
Code
import numpy as np
arr = np.array([[11, 12], [13, 14], [15, 16], [17, 18], [19, 20], [21, 22]])
newarr = np.array_split(arr, 3)
print(newarr)
the output will be
[array([[11, 12],
[13, 14]]), array([[15, 16],
[17, 18]]), array([[19, 20],
[21, 22]])]
The example above returns three 2-D arrays.
Let's look at another example, this time each element in the 2-D arrays contains 3 elements.
Example 5: Split the 2-D array into three 2-D arrays.
Code
import numpy as np
arr = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22], [23, 24, 25], [26, 27, 28]])
newarr = np.array_split(arr, 3)
print(newarr)
the output will be
[array([[11, 12, 13],
[14, 15, 16]]), array([[17, 18, 19],
[20, 21, 22]]), array([[23, 24, 25],
[26, 27, 28]])]
The example above returns three 2-D arrays.
In addition, you can specify which axis you want to do the split around.
The example below also returns three 2-D arrays, but they are split along the row (axis=1).
Example 6: Split the 2-D array into three 2-D arrays along rows.
Code
import numpy as np
arr = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22], [23, 24, 25], [26, 27, 28]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)
the output will be
[array([[11],
[14],
[17],
[20],
[23],
[26]]), array([[12],
[15],
[18],
[21],
[24],
[27]]), array([[13],
[16],
[19],
[22],
[25],
[28]])]
Note: An alternate solution is using hsplit() opposite of hstack().
Example 7: Use the hsplit() method to split the 2-D array into three 2-D arrays along rows.
Code
import numpy as np
arr = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22], [23, 24, 25], [26, 27, 28]])
newarr = np.hsplit(arr, 3)
print(newarr)
the output will be
[array([[11],
[14],
[17],
[20],
[23],
[26]]), array([[12],
[15],
[18],
[21],
[24],
[27]]), array([[13],
[16],
[19],
[22],
[25],
[28]])]
Note: Similar alternates to vstack() and dstack() are available as vsplit() and dsplit().