Skip to content
Advertisement

Creating 3d Tensor Array from 2d Array (Python)

I have two numpy arrays (4×4 each). I would like to concatenate them to a tensor of (4x4x2) in which the first ‘sheet’ is the first array, second ‘sheet’ is the second array, etc. However, when I try np.stack the output of d[1] is not showing the correct values of the first matrix.

JavaScript

Advertisement

Answer

If you do np.dstack((x, y)), which is the same as the more explicit np.stack((x, y), axis=-1), you are concatenating along the last, not the first axis (i.e., the one with size 2):

JavaScript

Ellipsis (...) is a python object that means “: as many times as necessary” when used in an index. For a 3D array, you can equivalently access the leaves as

JavaScript

If you want to access the leaves along the first axis, your array must be (2, 4, 4):

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement