Equal row batches
Partition tabular data into equal row blocks for batch processing.
NumPy · Function
Splits an array into multiple sub-arrays row-wise. It always operates along the first axis and accepts either an equal-section count or explicit split positions.
np.vsplit divides an array at row boundaries. An integer requests that many equal sections, while an array-like sequence supplies split positions; the operation always uses the first axis, including for arrays with more than two dimensions.
numpy.vsplit(ary, indices_or_sections)| Name | Type | Description |
|---|---|---|
ary | array_like | The array to split. If ary is a 1-D array, it is treated as a row vector. |
indices_or_sections | int or 1-D array | If an integer, the number of equal sections; if an array of sorted integers, the split positions along the first axis. |
A list of sub-arrays.
np.vsplit to turn one array into a list of row-oriented sub-arrays.np.split with axis=0.vsplit still splits only the first dimension, not an arbitrary axis.import numpy as np
x = np.arange(12).reshape(3, 4)
parts = np.vsplit(x, 3)
print([part.shape for part in parts])
# [(1, 4), (1, 4), (1, 4)]This creates three row-wise sub-arrays, each containing one row of the input.
Partition tabular data into equal row blocks for batch processing.
Cut a multidimensional tensor at known first-axis boundaries.
Separate a one-dimensional sequence into vertical-style row segments.
np.vsplit do, and how is it related to np.split?np.vsplit(ary, indices_or_sections) splits an array into multiple sub-arrays along the first axis. It is equivalent to calling np.split(ary, indices_or_sections, axis=0).
Follow-up: How does an integer differ from explicit indices? An integer requests equal sections, whereas an index sequence marks the boundaries and can produce uneven sections.
indices_or_sections take?An integer specifies the number of equal sections. A sequence such as [3, 6] specifies row boundaries, producing sections before 3, from 3 to 6, and after 6.
Follow-up: What happens when a boundary reaches the end? The resulting list can include an empty trailing array, as shown by the documented example using indices [3, 6].
vsplit behave for arrays with more than two dimensions?The first axis is always selected, regardless of the array's dimensionality. Thus, for a three-dimensional array, each returned array preserves the remaining dimensions while receiving a subset of the first-axis entries.
Follow-up: Which function should be used when the desired axis is not axis 0? Use np.split and pass the required axis explicitly.
numpy.array_splitnumpy.splitnumpy.hsplit