将2D阵列与3D阵列相结合

时间:2013-09-03 14:59:13

标签: python arrays numpy

你好我有3个numpy数组,如下所示。

>>> 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.]]

我想将它们合并为

[[[  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.]]]

那就是我想从每个数组中取一行。 谁能告诉我一个简单的方法呢? 我已经尝试了hstackvstack。但他们没有给出理想的结果。

谢谢!

6 个答案:

答案 0 :(得分:6)

使用np.stack可以解决这个问题:

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

答案 1 :(得分:5)

使用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]]])

答案 2 :(得分:4)

>>> 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.]]])

答案 3 :(得分:2)

我认为我有一些有用的东西:

>>> 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]]]

答案 4 :(得分:2)

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

我使用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

看起来@Viktor Kerkez的答案最快。

答案 5 :(得分:2)

无需使用vstackhstack。只需使用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]]])