1

I have two numpy arrays (4x4 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.

import numpy as np
x = array([[ 3.38286851e-02, -6.11905173e-05, -9.08147798e-03,
        -2.46860166e-02],
       [-6.11905173e-05,  1.74237508e-03, -4.52140165e-04,
        -1.22904439e-03],
       [-9.08147798e-03, -4.52140165e-04,  1.91939979e-01,
        -1.82406361e-01],
       [-2.46860166e-02, -1.22904439e-03, -1.82406361e-01,
         2.08321422e-01]])
print(np.shape(x)) # 4 x 4

y = array([[ 6.76573701e-02, -1.22381035e-04, -1.81629560e-02,
        -4.93720331e-02],
       [-1.22381035e-04,  3.48475015e-03, -9.04280330e-04,
        -2.45808879e-03],
       [-1.81629560e-02, -9.04280330e-04,  3.83879959e-01,
        -3.64812722e-01],
       [-4.93720331e-02, -2.45808879e-03, -3.64812722e-01,
         4.16642844e-01]])
print(np.shape(y)) # 4 x 4

d = np.dstack((x,y))
np.shape(d) # indeed it is 4,4,2... but if I do d[1] then it is not the first x matrix.
d[1] # should be y
2
  • 1
    You expect d[1] to be y, no? d[0] -> x? Commented Feb 2, 2022 at 1:31
  • you are right! making correction to comment now. thanks! Commented Feb 2, 2022 at 1:32

2 Answers 2

1

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):

(x == d[..., 0]).all()
(y == d[..., 1]).all()

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

d[:, :, 0]
d[:, :, 1]

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

d = np.stack((x, y), axis=0)
(x == d[0]).all()
(y == d[1]).all()
Sign up to request clarification or add additional context in comments.

6 Comments

Awesome answer! What if I don't have an initial array and want to stack iteratively?
@sarah. That's a frequent question. Either estimate how much you'll need and pre-allocate, or append to a list until you're done. Reallocating the entire buffer over and over is going to be a major bottleneck.
That is true, how would we allocate a 4x4 initial array and then stack the subsequent on top?
@sarah. I highly recommend you don't do that. But you can do np.zeros((0, 4, 4)) or (4, 4, 0). But again, use a list or pre-allocate the maximum size instead.
@sarah. The first by slicing: d = d[1:]. Interior ones, by np.delete. Again, use a list until the size stops changing. Deleting a 4-byte reference from a list is much cheaper than moving a multidimensional array.
|
1

Use np.stack instead of np.dstack:

>>> d = np.stack([y, x])

>>> np.all(d[0] == y)
True

>>> np.all(d[1] == x)
True

>>> d.shape
(2, 4, 4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.