combining 2D arrays to 3D arrays

前端 未结 6 740
北恋
北恋 2021-01-03 10:32

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


        
6条回答
  •  别那么骄傲
    2021-01-03 11:20

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

提交回复
热议问题