Data often arrives in pieces you assemble into one array — batches of rows, columns of features. NumPy joins arrays along an existing axis (concatenate) or along a brand-new one (stack).
concatenate
a = np.array([[1, 2], [3, 4]])b = np.array([[5, 6]])np.concatenate([a, b], axis=0) # stack rows → 3×2np.concatenate([a, a], axis=1) # widen columns → 2×4
vstack, hstack, stack
np.vstack([a, b]) # vertical (rows)np.hstack([a, a]) # horizontal (columns)# stack adds a NEW axis — a list of 2-D arrays → one 3-D arraynp.stack([img1, img2, img3]) # shape (3, H, W)
Key takeaways
- 01np.concatenate joins arrays along an existing axis; shapes must match except on that axis.
- 02vstack joins rows, hstack joins columns — readable shorthands.
- 03np.stack creates a NEW axis, turning a list of arrays into a higher-dimensional batch.
- 04Choose the axis deliberately — it decides whether you grow rows or columns.
