split() (NumPy)
np.split() divides an array into multiple sub-arrays along an axis, returning a list of pieces. It is the inverse of concatenate, with hsplit and vsplit for the horizontal and vertical cases.
NumPy
np.split(a, 3) breaks an array into 3 equal pieces, which requires the length along that axis to divide evenly — otherwise it raises an error. When the sizes don't divide evenly, np.array_split does the same job but allows uneven chunks, so it never errors on indivisible lengths.
You can also pass a list of indices instead of a count to split at specific positions: np.split(a, [2, 5]) cuts before index 2 and before index 5, giving three pieces of possibly different sizes. hsplit and vsplit are shortcuts for splitting along columns and rows.
Splitting returns views into the original array where possible, so the pieces share memory with the source unless you copy them.
Syntax
np.split(a, sections, axis=0) | np.array_split(a, n)Examples
Python
import numpy as np
print(np.split(np.arange(6), 3))Output:
[array([0, 1]), array([2, 3]), array([4, 5])]