添加特定值作为 numpy 数组的新维度
Add a specifc value as a new dimension of a numpy array
读取图像后,我有一个形状为 (224,224,3) 的 numpy 数组。但是我想将其转换为 (4,224,224,3) 的形状。
我想重复相同的值。
我正在尝试如下所示进行追加,但它不起作用。
np.append(image,[[[4]]],axis=1)
相反,它会抛出以下错误
ValueError: all the input arrays must have same number of dimensions
我希望输出形状为 (4,224,224,3)
你能指导我如何操作吗?
您可以使用 np.repeat
将轴设置为 0
:
out = np.repeat([image], 4, axis=0)
out.shape
# (4, 224, 224, 3)
读取图像后,我有一个形状为 (224,224,3) 的 numpy 数组。但是我想将其转换为 (4,224,224,3) 的形状。
我想重复相同的值。
我正在尝试如下所示进行追加,但它不起作用。
np.append(image,[[[4]]],axis=1)
相反,它会抛出以下错误
ValueError: all the input arrays must have same number of dimensions
我希望输出形状为 (4,224,224,3)
你能指导我如何操作吗?
您可以使用 np.repeat
将轴设置为 0
:
out = np.repeat([image], 4, axis=0)
out.shape
# (4, 224, 224, 3)