Equal-sized batching
Divide rows or columns into equally sized blocks for batch processing or parallel work.
NumPy · Function
Splits an ndarray into multiple sub-arrays along a chosen axis. It supports equal-count splitting or explicit sorted boundaries, and returns views of the original array.
Pass an array, either a number of equal sections or sorted split positions, and the axis to divide. The function returns a list of sub-arrays that are views into the original array; integer section counts must divide the selected axis evenly.
numpy.split(ary, indices_or_sections, axis=0)| Name | Type | Description |
|---|---|---|
ary | ndarray | Array to be divided into sub-arrays. |
indices_or_sections | int or 1-D array | An integer N for equal sections, or a one-dimensional array of sorted integers marking split positions. |
axis | int, optional | Dimension along which to split; defaults to 0. |
sub-arrays — list of ndarrays: a list of sub-arrays as views into ary.
import numpy as np
x = np.arange(12).reshape(3, 4)
rows = np.split(x, 3, axis=0)
columns = np.split(x, 2, axis=1)
print(rows[0])
print(columns[0])The array is divided evenly into three row sub-arrays and two column sub-arrays.
Divide rows or columns into equally sized blocks for batch processing or parallel work.
Cut an array at known offsets to extract segments with specific lengths.
Partition multidimensional data along a selected dimension.
It returns a list of sub-arrays produced by dividing ary along axis. The returned sub-arrays are views into the original array, not independent copies.
Follow-up: How can you preserve unequal final sections? Use explicit sorted integer boundaries, or choose numpy.array_split when an integer count should allow unequal sections.
An integer N requests N equal sections along the selected axis. If equal division is impossible, the function raises ValueError.
Follow-up: What happens with explicit boundaries beyond the axis length? The documented example shows that they can produce an empty trailing sub-array.
axis selects the dimension being divided and defaults to 0. For a two-dimensional array, axis=0 splits rows, while axis=1 splits columns.
Follow-up: How would you split a three-dimensional array along its last dimension? Pass axis=-1, provided that dimension and the requested boundaries are valid.
numpy.array_splitnumpy.hsplitnumpy.vsplitnumpy.dsplit