np.stack: Joining on a New Axis
np.stack combines same-shape arrays along a brand-new axis (dimensions go up by one) — how it differs from concatenate, and what the axis argument controls.
You have the squad's scores for 2023 in one (2, 3) table and for 2024 in another (2, 3) table. You don't want a longer table — you want a 3D array where the first index picks the season: seasons[0] is 2023, seasons[1] is 2024. That is exactly what np.stack does.
Definition
np.stackjoins a sequence of arrays of exactly the same shape along a new axis. The result has one more dimension than the inputs, and the new axis has length equal to the number of arrays stacked.
- Same shape — every input must match exactly; no size can differ.
- New axis —
concatenatereuses an existing axis;stackinserts a fresh one. Two(2, 3)arrays →(2, 2, 3). - axis argument — chooses where the new axis goes in the result shape (
0= front,-1= end).
Syntax
python
np.stack(arrays, axis=0, out=None, *, dtype=None, casting="same_kind")| Parameter | Meaning | Default |
|---|---|---|
arrays | sequence (tuple/list) of arrays, all the same shape | required |
axis | position of the new axis in the result (0 … ndim, or negative) | 0 |
out | optional array to put the result in | None |
dtype | force the result dtype | None |
casting | dtype conversion rule | "same_kind" |
Example — 1D: stack two players
python
import numpy as np
virat = np.array([72, 45, 88, 31])
rohit = np.array([56, 0, 74, 88])
s0 = np.stack((virat, rohit))
print(s0)
print(s0.shape)
s1 = np.stack((virat, rohit), axis=1)
print(s1)
print(s1.shape)
print(np.concatenate((virat, rohit)).shape)text
[[72 45 88 31]
[56 0 74 88]]
(2, 4)
[[72 56]
[45 0]
[88 74]
[31 88]]
(4, 2)
(8,)How it works: two (4,) arrays, so the result is 2D.
axis=0→ new axis in front →(2, 4): same asvstack.axis=1→ new axis at the end →(4, 2): same ascolumn_stack.concatenatestays 1D →(8,).
Example — 2D: stack seasons into a 3D array
python
s2023 = np.array([[72, 45, 88],
[56, 0, 74]])
s2024 = np.array([[31, 64, 0],
[88, 32, 45]])
seasons = np.stack((s2023, s2024))
print(seasons.shape)
print(seasons)
print(seasons.sum(axis=0))text
(2, 2, 3)
[[[72 45 88]
[56 0 74]]
[[31 64 0]
[88 32 45]]]
[[103 109 88]
[144 32 119]]How it works: shape (2, 2, 3) = (season, player, match). seasons[1, 0, 2] is Virat's 3rd match of 2024. Summing over axis=0 (the new "season" axis) adds the two seasons element by element.
Where does the new axis go?
python
print(np.stack((s2023, s2024), axis=2).shape)
print(np.stack((s2023, s2024), axis=-1)[0])
print(np.concatenate((s2023, s2024)).shape)text
(2, 3, 2)
[[72 31]
[45 64]
[88 0]]
(4, 3)With axis=-1 the season becomes the last axis: result[0] is Virat, and each row pairs his 2023 and 2024 score for that match.
| Inputs (each) | axis | Result shape |
|---|---|---|
(2, 3) × 2 arrays | 0 | (2, 2, 3) |
(2, 3) × 2 arrays | 1 | (2, 2, 3) (new axis in the middle) |
(2, 3) × 2 arrays | 2 or -1 | (2, 3, 2) |
(4,) × 5 arrays | 0 | (5, 4) |
(4,) × 5 arrays | 1 | (4, 5) |
stack vs concatenate
np.concatenate | np.stack | |
|---|---|---|
| Axis used | an existing axis | a new axis |
| Number of dimensions | stays the same | goes up by 1 |
| Shape requirement | equal except on the join axis | exactly equal |
(2,3) + (2,3), axis 0 | (4, 3) | (2, 2, 3) |
| Typical use | more rows / more columns | batch of images, list of seasons, list of samples |
Tip:
np.stackis how you turn a Python list of same-shape arrays into one array — e.g. a list of 100 image arrays(28, 28)→np.stack(images)gives(100, 28, 28).
Common mistakes
- ❌ Stacking arrays of different shapes. ✅
np.stackraisesValueError: all input arrays must have the same shape— useconcatenateinstead if you want to join different lengths. - ❌ Using
stackwhen you only wanted more rows. ✅concatenate/vstackkeep 2D;stackmakes it 3D. - ❌ Forgetting the new axis length equals the number of arrays, not their size.
Interview tip
Q: You have 3 RGB images, each (64, 64, 3). What do np.stack(imgs) and np.concatenate(imgs) return?
A: stack → (3, 64, 64, 3), a batch of 3 images. concatenate → (192, 64, 3), the images glued vertically into one tall image.
Key points to remember
- np.stack joins same-shape arrays along a NEW axis, so the result has one more dimension.
- The new axis has length = number of arrays stacked; the axis argument chooses where it goes.
- Every input must have exactly the same shape, otherwise 'all input arrays must have the same shape'.
- For 1D inputs: stack(axis=0) equals vstack, stack(axis=1) equals column_stack.
- concatenate keeps the number of dimensions: (2,3)+(2,3) → (4,3); stack gives (2,2,3).
- np.stack is the standard way to turn a list of equal-shape arrays (images, seasons) into one array.
Try it yourself
stack vs concatenate on two seasons
List of arrays to one array
Practice: three seasons in 3D
Revision Flashcards
Tap a card to flip it and see the answer.
