Skip to main content
Albero Academy

Splitting Arrays

Cut arrays apart with split, hsplit, vsplit and array_split.

Manipulation7 min

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

hsplit, vsplit
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

array_split
np.array_split(np.arange(10), 3) # [0-3], [4-6], [7-9] — uneven, no error

Key takeaways

  1. 01split/hsplit/vsplit cut an array into sub-arrays, evenly or at given indices.
  2. 02hsplit divides columns; vsplit divides rows.
  3. 03np.split errors on uneven divisions; np.array_split tolerates them.
  4. 04Splitting is the inverse of concatenate/stack.

Finished this chapter?

Mark it complete to track your progress through NumPy.