numpy.concatenate((a1, a2, …), axis=0)
Join a sequence of arrays along an existing axis.(按轴axis连接array组成一个新的array)
The arrays must have the same shape, except in the dimension corresponding to axis
axis:default is 0
示例代码:
Numpy 二维和一维的区别
>>> b = np.array([[5,6]]) b是二维的
>>> b.shape
(1, 2)
>>> b = np.array([5,6]) b是一维的
>>> b.shape
(2,)
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]]) b是一个二维array,b.shape为(1,2)
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1) b.T是一个二维array,其shape为(2,1)
array([[1, 2, 5],
[3, 4, 6]])
一维array时,所有数字相连接
>>> a=np.array([1,2,3])
>>> b=np.array([11,22,33])
>>> c=np.array([44,55,66])
>>> np.concatenate((a,b,c),axis=0)
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66])
推广至多维
例如4维,a.shape为(4,5,6,10),b.shape为(4,5,6,20)
np.concatenate([a,b], axis=3) # 返回张量的shape为(4,5,6,30)
参考:
[1] https://www.cnblogs.com/ymjyqsx/p/6472507.html