0

I'm trying to mimic the combination of the aa and bb arrays shown below using zip() within a for block:

import numpy as np

aa = np.random.uniform(0., 1., (3, 566))
bb = np.random.uniform(0., 1., (3, 566))

cc = []
for a, b in list(zip(list(zip(*aa)), list(zip(*bb)))):
    cc.append(list(zip(*[a, b])))
cc = np.array(cc)

print(cc.shape)
(566, 3, 2)

I've tried vstack, hstack, column_stack, all of them combined with .reshape() to no avail. Obviously, not only the final shape should be equal, but the array itself too.

What is the proper numpy way to do this?

1
  • 1
    Another variation on stack: np.stack([aa,bb],1).transpose(2,0,1)`. This gets the (3,2) order right, and then transposes the 566 to the start. Commented Aug 30, 2018 at 19:39

1 Answer 1

1

You can transpose aa and bb and then use numpy.dstack(stack arrays along third axis), i.e. np.dstack([aa.T, bb.T]):

np.dstack([aa.T, bb.T]).shape
# (566, 3, 2)

(np.dstack([aa.T, bb.T]) == cc).all()
# True

Or use np.stack(..., axis=-1):

(np.stack([aa.T, bb.T], axis=-1) == cc).all()
# True
Sign up to request clarification or add additional context in comments.

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.