Splitting NumPy Arrays
Array splitting is the opposite operation of array join. The join operation combines two or more arrays into one and Splitting divides one array into two or more arrays. The array_split() method is used for dividing an array.
The syntax of this method is,
numpy.array_split(ary, indices_or_sections, axis)
indices_or_sections parameter can be an integer that does not equally divide that axis. For example,
import numpy as np nums1 = np.array([10, 20, 30, 40, 50, 60, 70, 80]) nums = np.array_split(nums1,3) print('======split=======\n',nums) #output ======split======= [array([10, 20, 30]), array([40, 50, 60]), array([70, 80])]* We can also use split(ary, indices_or_sections, axis) method, this works similar to array_split() method. The only difference is that it can only split the array into equal parts.
If the array does not have enough items to be split equally, it can adjust division automatically.
The split arrays can be accessed easily.
import numpy as np nums1 = np.array([10, 20, 30, 40, 50, 60, 70, 80]) nums = np.array_split(nums1,5) print(nums[0]) print(nums[1]) print(nums[2]) print(nums[3]) print(nums[4]) #output [10 20] [30 40] [50 60] [70] [80]
Splitting 2-D array
import numpy as np #2-D array nums1 = np.array([[10, 20, 30],[40, 50, 60], [70, 80,90],[5, 15, 25],[35, 45, 55],[65, 75, 85]]) nums = np.array_split(nums1,3, axis=0) print('======Axis=0=======\n') print(nums[0]) print(nums[1]) print(nums[2]) nums = np.array_split(nums1,3, axis=1) print('======Axis=1=======\n') print(nums[0]) print(nums[1]) print(nums[2]) #Output ======Axis=0======= [[10 20 30] [40 50 60]] [[70 80 90] [ 5 15 25]] [[35 45 55] [65 75 85]] ======Axis=0======= [[10] [40] [70] [ 5] [35] [65]] [[20] [50] [80] [15] [45] [75]] [[30] [60] [90] [25] [55] [85]]
hsplit()
We can also use hsplit() function to split the array horizontally.
import numpy as np nums1 = np.array([[10, 20, 30],[40, 50, 60], [70, 80,90],[5, 15, 25],[35, 45, 55],[65, 75, 85]]) nums = np.hsplit(nums1,3) print('======hsplit=======\n') print(nums[0]) print(nums[1]) print(nums[2]) #Output ======hsplit======= [[10] [40] [70] [ 5] [35] [65]] [[20] [50] [80] [15] [45] [75]] [[30] [60] [90] [25] [55] [85]]
vsplit()
We can also use vsplit() function to split the array vertically.
import numpy as np nums1 = np.array([[10, 20, 30],[40, 50, 60], [70, 80,90],[5, 15, 25],[35, 45, 55],[65, 75, 85]]) nums = np.vsplit(nums1,3) print('======Vsplit=======\n') print(nums[0]) print(nums[1]) print(nums[2]) #Output ======Vsplit======= [[10 20 30] [40 50 60]] [[70 80 90] [ 5 15 25]] [[35 45 55] [65 75 85]]
dsplit()
Similar to the dstack() method to join operations, we have dsplit() method to split the array depth-wise.
The dsplit() works only for 3-D Array or more.
import numpy as np nums1 = np.array([[[10, 20, 30],[40, 50, 60]],[[70, 80,90],[5, 15, 25]],[[35, 45, 55],[65, 75, 85]]]) nums = np.dsplit(nums1,3) print('======dsplit=======\n') print(nums[0]) print(nums[1]) print(nums[2]) #Output ======dsplit======= [[[10] [40]] [[70] [ 5]] [[35] [65]]] [[[20] [50]] [[80] [15]] [[45] [75]]] [[[30] [60]] [[90] [25]] [[55] [85]]]