🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.stack()

AI & DATA SCIENCE // np-stack

np.stack() joins a sequence of arrays along a brand-new axis, producing a result with one more dimension than the individual input arrays.

Syntax

np.stack(arrays, axis=0)

Deep Dive Course

Every array passed to stack() must have exactly the same shape, since stack() doesn't extend an existing axis, it introduces an entirely new one at the position given by axis and lines up the input arrays along it — stacking a list of 1D arrays with axis=0 produces a 2D array where each input becomes one row. This makes stack() the right tool for combining several separately-computed arrays of identical shape into one higher-dimensional array, such as collecting a batch of individually processed samples.

1Understanding np.stack()

Every array passed to stack() must have exactly the same shape, since stack() doesn't extend an existing axis, it introduces an entirely new one at the position given by axis and lines up the input arrays along it — stacking a list of 1D arrays with axis=0 produces a 2D array where each input becomes one row. This makes stack() the right tool for combining several separately-computed arrays of identical shape into one higher-dimensional array, such as collecting a batch of individually processed samples.

💡

If you need to combine same-shaped arrays into a new leading 'batch' dimension, like preparing individual samples for a machine learning model, np.stack(arrays) with the default axis=0 is exactly that operation.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.stack((a, b)))
localhost:3000

2Practical Example

Here is a real-world application of np.stack() showing how it is used in production NumPy code.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked = np.stack((a, b), axis=1)
print(stacked)
localhost:3000

3Best Practices

Follow these guidelines when working with np.stack():

1. Use stack() specifically when you want to introduce a new dimension, such as a batch axis, rather than extend an existing one

2. Make sure every input array has exactly the same shape before calling stack(), since unlike concatenate(), there's no 'all axes except one' flexibility

3. Choose the axis parameter deliberately to control where the new dimension appears in the resulting shape, not just accept the default

⚠️

Tip: If you need to combine same-shaped arrays into a new leading 'batch' dimension, like preparing individual samples for a machine learning model, np.stack(arrays) with the default axis=0 is exactly that operation.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.stack((a, b)))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.stack((a, b)))
Example 02Advanced Example
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked = np.stack((a, b), axis=1)
print(stacked)

Best Practices

  • Use stack() specifically when you want to introduce a new dimension, such as a batch axis, rather than extend an existing one
  • Make sure every input array has exactly the same shape before calling stack(), since unlike concatenate(), there's no 'all axes except one' flexibility
  • Choose the axis parameter deliberately to control where the new dimension appears in the resulting shape, not just accept the default

Interview Question

Why does np.stack() require every input array to have exactly the same shape, while np.concatenate() allows some flexibility?

Hint: Think about what a 'new dimension' actually requires from the pieces being combined.

Stacking arranges the input arrays along a brand-new axis, which only makes sense geometrically if every array being stacked has an identical shape, the same way you can only stack physical sheets of paper neatly if they're all the same size. concatenate() instead only extends an axis that already exists, so it only requires the input arrays to match along the axes not being joined, allowing them to differ in size specifically along the axis being concatenated.

Exercises

MediumPractice using np.stack() in a real scenario.
View Solution
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.stack((a, b)))

Frequently Asked Questions

Why does np.stack() require every input array to have exactly the same shape, while np.concatenate() allows some flexibility?

Stacking arranges the input arrays along a brand-new axis, which only makes sense geometrically if every array being stacked has an identical shape, the same way you can only stack physical sheets of paper neatly if they're all the same size. concatenate() instead only extends an axis that already exists, so it only requires the input arrays to match along the axes not being joined, allowing them to differ in size specifically along the axis being concatenated.

Related Functions

np-concatenatenp-vstacknp-hstack