【发布时间】:2021-03-20 20:02:17
【问题描述】:
我有两个长度为 32 且形状为 (32, 32, 3) 的 numpy 数组矩阵 A 和 B。我想将它们组合成一个新数组,以便我的新数组的维度为 (2, 32, 32,3)。
使用 np.连接会引发错误。
【问题讨论】:
-
concatenate抛出什么错误?我们想要详细信息
标签: python numpy numpy-ndarray numpy-slicing
我有两个长度为 32 且形状为 (32, 32, 3) 的 numpy 数组矩阵 A 和 B。我想将它们组合成一个新数组,以便我的新数组的维度为 (2, 32, 32,3)。
使用 np.连接会引发错误。
【问题讨论】:
concatenate 抛出什么错误?我们想要详细信息
标签: python numpy numpy-ndarray numpy-slicing
使用np.stack
def dim0_stack(*arrays):
return np.stack(arrays, axis = 0)
【讨论】:
另一种方法:
a = np.random.randn(32, 32, 3)
b = np.random.randn(32, 32, 3)
c = np.concatenate([np.expand_dims(a,0), np.expand_dims(b, 0)], axis=0)
print(c.shape)
因为您提到使用concatenate,所以我想向您展示如何使用它。
【讨论】:
另一种更字面的方法
result = np.zeros((2, A.shape[0], A.shape[1], A.shape[2]))
result[0, :, :, :] = A
result[1, :, :, :] = B
【讨论】: