我想向现有数据立方体添加另一个数据“切片”:
import numpy as np
a = np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]])
print(a.shape) # (2, 3, 4)
b = np.array([[7, 7, 7], [8, 8, 8]])
print(b.shape) # (2, 3)
c = np.concatenate([a, b], axis=2) # ValueError: all the input arrays must have same number of dimensions
print(c.shape) # wanted result: (2, 3, 5)
所以我基本上想通过将最后一个维度扩展到 2x3x5 来将 2x3 数组添加到 2x3x4 数组。值错误对我来说不太有意义,因为数组不能是相同的形状?我在这里做错了什么?
请您参考如下方法:
在 b 的末尾添加一个新轴,然后连接 -
np.concatenate((a, b[...,None]), axis=2)
因此,对于给定的样本 -
In [95]: np.concatenate((a, b[...,None]), axis=2).shape
Out[95]: (2, 3, 5)
