Stack equally shaped 2-D NumPy arrays into a 3-D tensor
Quick Overview
Write a NumPy function that turns a list of equally shaped 2-D arrays into one 3-D array with the new axis first. It tests how adding a new axis differs from joining along an existing one, how dtypes carry through, and which inputs make the operation fail.
Stack equally shaped 2-D NumPy arrays into a 3-D tensor
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `stack_to_tensor(mats)`. It takes a Python list of `m` equally shaped 2-D NumPy arrays of shape `(h, w)` and returns a single 3-D `ndarray` of shape `(m, h, w)`.
Requirements:
- The new axis is axis 0, so `result[i]` equals the i-th input array.
- Preserve the dtype and element values of the inputs.
Example:
```python
import numpy as np
a = np.zeros((2, 3))
b = np.ones((2, 3))
stack_to_tensor([a, b]).shape
# -> (2, 2, 3)
```
```hint Which joining function
NumPy has more than one function that joins arrays. Check which one keeps the rank of its inputs and which one adds a dimension.
```
### What a Strong Answer Covers
- A single-call implementation that puts the new axis first and returns shape `(m, h, w)`
- Why joining along an existing axis gives the wrong shape here
- What happens to the dtype when all inputs share one, and when they do not
- Behavior on an empty list and on arrays with different shapes
### Follow-up Questions
- How does `np.stack` differ from `np.concatenate` here?
- What should happen if the input arrays have mismatched shapes?
- If the `m` arrays arrive one at a time from a loader and you know `m` up front, how would you build the tensor without holding a Python list of all of them?
Overview: Write a NumPy function that turns a list of equally shaped 2-D arrays into one 3-D array with the new axis first. It tests how adding a new axis differs from joining along an existing one, how dtypes carry through, and which inputs make the operation fail.
Implement stack_to_tensor(mats). It takes a Python list of m equally shaped 2-D NumPy arrays of shape (h, w) and returns a single 3-D ndarray of shape (m, h, w).
Requirements:
The new axis is axis 0, so
result[i]
equals the i-th input array.
Preserve the dtype and element values of the inputs.
Example:
import numpy as np
a = np.zeros((2, 3))
b = np.ones((2, 3))
stack_to_tensor([a, b]).shape
# -> (2, 2, 3)
What a Strong Answer Covers Guidance
A single-call implementation that puts the new axis first and returns shape
(m, h, w)
Why joining along an existing axis gives the wrong shape here
What happens to the dtype when all inputs share one, and when they do not
Behavior on an empty list and on arrays with different shapes
Follow-up Questions Guidance
How does
np.stack
differ from
np.concatenate
here?
What should happen if the input arrays have mismatched shapes?
If the
m
arrays arrive one at a time from a loader and you know
m
up front, how would you build the tensor without holding a Python list of all of them?