Reputation: 3462
Hello I have 3 numpy arrays as given below.
>>> print A
[[ 1. 0. 0.]
[ 3. 0. 0.]
[ 5. 2. 0.]
[ 2. 0. 0.]
[ 1. 2. 1.]]
>>> print B
[[ 5. 9. 9.]
[ 37. 8. 9.]
[ 49. 8. 3.]
[ 3. 3. 1.]
[ 4. 4. 5.]]
>>>
>>> print C
[[ 0. 0. 0.]
[ 0. 6. 0.]
[ 1. 4. 6.]
[ 6. 2. 0.]
[ 0. 5. 4.]]
I would like to combine them as
[[[ 1. 0. 0.]
[ 5. 9. 9.]
[ 0. 0. 0.]]
[[ 3. 0. 0.]
[ 37. 8. 9.]
[ 0. 6. 0.]]
[[ 5. 2. 0.]
[ 49. 8. 3.]
[ 1. 4. 6.]]
[[ 2. 0. 0.]
[ 3. 3. 1.]
[ 6. 2. 0.]]
[[ 1. 2. 1.]
[ 4. 4. 5.]
[ 0. 5. 4.]]]
That is I would like to take one row from each array.
Could anyone tell me a simple way to do it?
I already tried hstack
, vstack
. But they are not giving the desired result.
Thanks !
Upvotes: 5
Views: 12501
Reputation: 19537
A solution using numpy dstack
:
>>> import numpy as np
>>> np.dstack((a,b,c)).swapaxes(1,2)
array([[[ 1, 0, 0],
[ 5, 9, 9],
[ 0, 0, 0]],
[[ 3, 0, 0],
[37, 8, 9],
[ 0, 6, 0]],
[[ 5, 2, 0],
[49, 8, 3],
[ 1, 4, 6]],
[[ 2, 0, 0],
[ 3, 3, 1],
[ 6, 2, 0]],
[[ 1, 2, 1],
[ 4, 4, 5],
[ 0, 5, 4]]])
Upvotes: 6
Reputation: 97565
Using np.stack
makes this trivial:
>>> np.stack([A, B, C], axis=1) # stack along a new axis in axis 1 of the result
array([[[ 1, 0, 0],
[ 5, 9, 9],
[ 0, 0, 0]],
[[ 3, 0, 0],
[37, 8, 9],
[ 0, 6, 0]],
[[ 5, 2, 0],
[49, 8, 3],
[ 1, 4, 6]],
[[ 2, 0, 0],
[ 3, 3, 1],
[ 6, 2, 0]],
[[ 1, 2, 1],
[ 4, 4, 5],
[ 0, 5, 4]]])
Upvotes: 12
Reputation: 31040
>>> import numpy as np
>>> A = np.array([[1,0,0],[3,0,0],[5,2,0],[2,0,0],[1,2,1]])
>>> B = np.array([[5,9,9],[37,8,9],[49,8,3],[3,3,1],[4,4,5]])
>>> C = np.array([[0,0,0],[0,6,0],[1,4,6],[6,2,0],[0,5,4]])
>>> np.array([A,B,C]).swapaxes(1,0)
array([[[ 1, 0, 0],
[ 5, 9, 9],
[ 0, 0, 0]],
[[ 3, 0, 0],
[37, 8, 9],
[ 0, 6, 0]],
[[ 5, 2, 0],
[49, 8, 3],
[ 1, 4, 6]],
[[ 2, 0, 0],
[ 3, 3, 1],
[ 6, 2, 0]],
[[ 1, 2, 1],
[ 4, 4, 5],
[ 0, 5, 4]]])
I did a comparison of the answers using Ipython %%timeit
:
np.array([A,B,C]).swapaxes(1,0)
100000 loops, best of 3: 18.2 us per loop
np.dstack((A,B,C)).swapaxes(1,2)
100000 loops, best of 3: 19.8 us per loop
np.hstack([A,B,C]).reshape((5,3,3))
100000 loops, best of 3: 14.8 us per loop
np.hstack([A[:, None, :], B[:, None, :], C[:, None, :]])
100000 loops, best of 3: 17.2 us per loop
It looks like @Viktor Kerkez's answer is fastest.
Upvotes: 2
Reputation: 54330
No need to use vstack
, hstack
. Just swap the axis using np.swapaxes
:
>>> d=array([a, b, c])
>>> d
array([[[ 1, 0, 0],
[ 3, 0, 0],
[ 5, 2, 0],
[ 2, 0, 0],
[ 1, 2, 1]],
[[ 5, 9, 9],
[37, 8, 9],
[49, 8, 3],
[ 3, 3, 1],
[ 4, 4, 5]],
[[ 0, 0, 0],
[ 0, 6, 0],
[ 1, 4, 6],
[ 6, 2, 0],
[ 0, 5, 4]]])
>>> swapaxes(d, 0, 1)
array([[[ 1, 0, 0],
[ 5, 9, 9],
[ 0, 0, 0]],
[[ 3, 0, 0],
[37, 8, 9],
[ 0, 6, 0]],
[[ 5, 2, 0],
[49, 8, 3],
[ 1, 4, 6]],
[[ 2, 0, 0],
[ 3, 3, 1],
[ 6, 2, 0]],
[[ 1, 2, 1],
[ 4, 4, 5],
[ 0, 5, 4]]])
Upvotes: 2
Reputation: 7592
I think I got something that works :
>>> print np.hstack([A[:, None, :], B[:, None, :], C[:, None, :]])
[[[ 1 0 0]
[ 5 9 9]
[ 0 0 0]]
[[ 3 0 0]
[37 8 9]
[ 0 6 0]]
[[ 5 2 0]
[49 8 3]
[ 1 4 6]]
[[ 2 0 0]
[ 3 3 1]
[ 6 2 0]]
[[ 1 2 1]
[ 4 4 5]
[ 0 5 4]]]
Upvotes: 2
Reputation: 46566
>>> np.hstack([a,b,c]).reshape((5,3,3))
array([[[ 1., 0., 0.],
[ 5., 9., 9.],
[ 0., 0., 0.]],
[[ 3., 0., 0.],
[ 37., 8., 9.],
[ 0., 6., 0.]],
[[ 5., 2., 0.],
[ 49., 8., 3.],
[ 1., 4., 6.]],
[[ 2., 0., 0.],
[ 3., 3., 1.],
[ 6., 2., 0.]],
[[ 1., 2., 1.],
[ 4., 4., 5.],
[ 0., 5., 4.]]])
Upvotes: 4