参考链接: numpy.concatenate
numpy.concatenate()函数接受一个数组的序列,其中的数组除了在拼接上的维度,其他的维度的形状必须相同.
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> a
array([[1, 2],
[3, 4]])
>>> b
array([[5, 6]])
>>> a.shape
(2, 2)
>>> b.shape
(1, 2)
>>>
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b), axis=0).shape
(3, 2)
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
>>>
>>> # 如果传入的参数axis=None,那么表示先展成一维再做拼接
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
>>>
>>>
>>> a = np.zeros((2,3,4,5,6))
>>> b = np.zeros((2,3,4,7,6))
>>> a.shape
(2, 3, 4, 5, 6)
>>> b.shape
(2, 3, 4, 7, 6)
>>> np.concatenate((a, b), axis=3).shape
(2, 3, 4, 12, 6)
>>> np.concatenate((a, b), axis=2)
Traceback (most recent call last):
File "" , line 1, in <module>
np.concatenate((a, b), axis=2)
File "<__array_function__ internals>", line 6, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 3, the array at index 0 has size 5 and the array at index 1 has size 7
>>>
>>> # 除了拼接的部分,其余部分的形状必须相同
>>>
>>>
>>>