np.stack 和 np.concatenate两个函数都是用来连接数组的, 但是他们之间还是有一些探讨之处,直接上代码,一看便知:
import numpy as np a = np.zeros(12).reshape(4,3,) b = np.arange(12).reshape(4,3) # for np.stack:all input arrays must have the same shape print(np.hstack((a, b)).shape) # (4, 6) print(np.vstack((a, b)).shape) # (8, 3) print(np.dstack((a, b)).shape) # (4, 3, 2) print(np.stack((a, b), axis=0).shape) # (2, 4, 3) print(np.stack((a, b), axis=1).shape) # (4, 2, 3) print(np.stack((a, b), axis=2).shape) # (4, 3, 2) a = np.zeros(12).reshape(2,3, 2) b = np.arange(6).reshape(2,3, 1) print(np.concatenate((a, b), axis=2).shape) # (2, 3, 3) # print(np.stack((a, b), axis=2).shape) # error print(np.dstack((a, b)).shape) # (2, 3, 3)