Reputation: 892
What's the most pythonic way to combine two numpy arrays, such that each array
(x,y,z) and (x1,y2,z3)
combined to
(2,x,y,z)
where the two lists are stacked? Is it np.stack?
EDIT: To clarify, both arrays are still there such that,
np.array([x,y,z],
[x1,y2,z2])
So when indexing onto the first element, you get the first tuple. The second element gets the second tuple.
Upvotes: 0
Views: 880
Reputation: 649
Yes, just use np.stack
and it works as you wanted!
for example:
x = np.arange(100).reshape(5, 2, 10)
x.shape # 5x2x10
np.stack((x, x)).shape # become 2x5x2x10
It concatenates its input along new axis created in front. But if you have lots of arrays wanted to be concatenate I suggest you to convert them to lists and then do concatenates and again convert the result to numpy array, it's way faster.
Upvotes: 1