Last updated
Splitting Arrays
Definition: Splitting is the opposite of joining — it breaks one array into several smaller ones. np.array_split divides an array into a chosen number of parts.
Example 1 — split into equal parts
import numpy as np a = np.array([1, 2, 3, 4, 5, 6]) parts = np.array_split(a, 3) print(parts) # [array([1,2]), array([3,4]), array([5,6])] print(parts[0]) # the first chunk
Example 2 — uneven splits are handled
Unlike strict split, array_split copes when the array does not divide evenly:
import numpy as np a = np.array([1, 2, 3, 4, 5]) print(np.array_split(a, 3)) # sizes 2, 2, 1
Splitting a 2D array
import numpy as np b = np.arange(1, 7).reshape(3, 2) print(np.array_split(b, 3)) # one row per part
💡 Tip: splitting is useful for breaking a dataset into batches — for example, dividing data for training and testing.
Try it Yourself
Output
Ad · responsive