Splitting Array using np.split( ) :
The split( ) function divides an array into equal-sized sub-array .
If splitted unequally , it raises error .
Syntax :
Python
np.split(array, sections, axis=0) Example :
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60]) result = np.split(arr, 3) print(result) #output [array([10, 20]), array([30, 40]), array([50, 60])] Splitting Array using np.array_split( ) :
np.array_split( ) can split arrays into unequal parts.
Example :
Python
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60]) result = np.array_split(arr, 4) print(result) #output [array([10, 20]), array([30, 40]), array([50]), array([60])] Splitting 2D Array Row-wise :
Python
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) result = np.split(arr, 2, axis=0) print(result) #output [array([[1, 2], [3, 4]]), array([[5, 6], [7, 8]])] Splitting 2D Array column-wise :
Python
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) result = np.split(arr, 2, axis=1) print(result) #output [array([[1], [3], [5], [7]]), array([[2], [4], [6], [8]])]