简体   繁体   English

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

[英]combining 2D arrays to 3D arrays

Hello I have 3 numpy arrays as given below. 你好,我有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.]]

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 . 我已经尝试过hstackvstack But they are not giving the desired result. 但他们没有给出理想的结果。

Thanks ! 谢谢 !

Using np.stack makes this trivial: 使用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]]])

A solution using numpy dstack : 使用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]]])
>>> 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.]]])

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]]]
>>> 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 : 我使用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. 看起来@Viktor Kerkez的答案是最快的。

No need to use vstack , hstack . 无需使用vstackhstack Just swap the axis using np.swapaxes : 只需使用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]]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM