numpy数组合并

  • 沿着新轴上合并:stack(arrays, axis=0)
沿着轴0合并
In [3]: import numpy as np
   ...: a = np.array([[1,2,3],[4,5,6]])
   ...: b = np.array([[6,5,6],[7,8,9]])
   ...: np.stack((a,b),axis=0)
   ...:
Out[3]:
array([[[1, 2, 3],
        [4, 5, 6]],

       [[6, 5, 6],
        [7, 8, 9]]])

In [4]: np.stack((a,b),axis=0).shape
Out[4]: (2, 2, 3)
沿着轴1合并
In [5]: np.stack((a,b),axis=1)
Out[5]:
array([[[1, 2, 3],
        [6, 5, 6]],

       [[4, 5, 6],
        [7, 8, 9]]])

In [6]: np.stack((a,b),axis=1).shape
Out[6]: (2, 2, 3)
沿着轴2合并
In [7]: np.stack((a,b),axis=2)
Out[7]:
array([[[1, 6],
        [2, 5],
        [3, 6]],

       [[4, 7],
        [5, 8],
        [6, 9]]])

In [8]: np.stack((a,b),axis=2).shape
Out[8]: (2, 3, 2)
  • 水平合并:vstack(tup)和row_stack(tup)
In [9]: np.vstack((a,b))
Out[9]:
array([[1, 2, 3],
       [4, 5, 6],
       [6, 5, 6],
       [7, 8, 9]])

In [10]: np.row_stack((a,b))
Out[10]:
array([[1, 2, 3],
       [4, 5, 6],
       [6, 5, 6],
       [7, 8, 9]])
  • 垂直合并:hstack(tup)和column_stack(tup)
In [11]: np.hstack((a,b))
Out[11]:
array([[1, 2, 3, 6, 5, 6],
       [4, 5, 6, 7, 8, 9]])

In [12]: np.column_stack((a,b))
Out[12]:
array([[1, 2, 3, 6, 5, 6],
       [4, 5, 6, 7, 8, 9]])






你可能感兴趣的:(Numpy)