The inverse of combining: splitting cuts one array into several — into equal parts or at explicit boundaries. It's how you separate features from labels, or chunk data for processing.
Splitting into parts
a = np.arange(12).reshape(3, 4)np.hsplit(a, 2) # two 3×2 halves (split the columns)np.vsplit(a, 3) # three 1×4 rows (split the rows)np.split(a, [1], axis=0) # split at row index 1
Uneven splits
np.array_split(np.arange(10), 3) # [0-3], [4-6], [7-9] — uneven, no error
Key takeaways
- 01split/hsplit/vsplit cut an array into sub-arrays, evenly or at given indices.
- 02hsplit divides columns; vsplit divides rows.
- 03np.split errors on uneven divisions; np.array_split tolerates them.
- 04Splitting is the inverse of concatenate/stack.
