1、np.vstack() :垂直合并
>>> import numpy as np
>>> A = np.array([1,1,1])
>>> B = np.array([2,2,2])
>>> print(np.vstack((A,B))) # vertical stack,属于一种上下合并,即对括号中的两个整体进行对应操作
[[1 1 1]
[2 2 2]] >>> C = np.vstack((A,B))
>>> print(A.shape,C.shape)
(3,) (2, 3)
2、np.hstack():水平合并
>>> D = np.hstack((A,B)) # horizontal stack,即左右合并
>>> print(D)
[1 1 1 2 2 2]
>>> print(A.shape,D.shape)
(3,) (6,)
3、np.newaxis():转置
>>> print(A[np.newaxis,:])
[[1 1 1]]
>>> print(A[np.newaxis,:].shape)
(1, 3)
>>> print(A[:,np.newaxis])
[[1]
[1]
[1]]
>>> print(A[:,np.newaxis].shape)
(3, 1) >>> A = np.array([1,1,1])[:,np.newaxis]
>>> B = np.array([2,2,2])[:,np.newaxis]
>>> C = np.vstack((A,B)) # vertical stack
>>> D = np.hstack((A,B)) # horizontal stack
>>> print(D)
[[1 2]
[1 2]
[1 2]]
>>> print(A.shape,D.shape)
(3, 1) (3, 2)
4、np.concatenate():针对多个矩阵或序列的合并操作
#axis参数很好的控制了矩阵的纵向或是横向打印,相比较vstack和hstack函数显得更加
>>> C = np.concatenate((A,B,B,A),axis=0)
>>> print(C)
[[1]
[1]
[1]
[2]
[2]
[2]
[2]
[2]
[2]
[1]
[1]
[1]] >>> D = np.concatenate((A,B,B,A),axis=1)
>>> print(D)
[[1 2 2 1]
[1 2 2 1]
[1 2 2 1]]