Append along a batch axis
Combine batches or partitions that share feature dimensions while increasing their sample dimension.
NumPy · Function
Joins a sequence of arrays along an existing axis. Inputs must match in every other dimension; axis=None flattens them before joining.
numpy.concatenate combines a sequence of array-like objects along an existing axis. Inputs must match in every dimension except the selected axis; setting axis to None flattens them first, while dtype and casting control the result's type conversion.
numpy.concatenate(*arrays, axis=0, out=None, dtype=None, casting='same_kind')| Name | Type | Description |
|---|---|---|
arrays (a1, a2, …) | sequence of array_like | Sequence of array-like objects to join. Shapes must match except along axis. |
axis | int or None, optional | Axis along which to join the arrays; None flattens inputs first. |
out | ndarray, optional | Destination array for the result; its shape must be correct. |
dtype | dtype, optional | Data type for the result. |
casting | {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional | Rules governing data-type casting. |
res: ndarray — the concatenated array.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
rows = np.concatenate((a, b), axis=0)
flat = np.concatenate((a, b), axis=None)
print(rows)
print(flat)Combines compatible arrays by rows, then demonstrates flattening all inputs before concatenation.
Combine batches or partitions that share feature dimensions while increasing their sample dimension.
Merge compatible matrices horizontally when their row counts match.
Turn several compatible inputs into one one-dimensional sequence.
It joins a sequence of arrays along an existing axis, using axis=0 by default. Every dimension other than the chosen axis must have matching size.
Follow-up: How does axis=None change the operation? It flattens the arrays before concatenating them, so the result is one-dimensional.
axis selects the dimension along which arrays are joined. If it is None, each array is flattened before the operation; otherwise the default is axis 0.
Follow-up: Why might concatenating two 2-D arrays on axis=1 fail? Their sizes must still match on axis 0, because only the selected axis may differ.
out supplies the destination ndarray and must have the shape that concatenate would produce without out. dtype requests the result data type, while casting specifies the permitted conversion policy.
Follow-up: What is the default casting policy? It is 'same_kind', which permits conversions within compatible NumPy type kinds rather than arbitrary unsafe conversions.
numpy.concatnumpy.stacknumpy.blocknumpy.ma.concatenate