如何在python中合并不同维度的数组?

Far*_*òrî 5 python image-processing deep-learning

我正在使用 keras 分析一些图像表示的数据集。我被困住了,我有两个不同尺寸的图像。请查看快照。features 有 14637 个尺寸为 (10,10,3) 的图像,features2 具有尺寸 (10,10,100)

在此输入图像描述

有什么方法可以将这两个数据合并/连接在一起吗?

Neb*_*Neb 3

如果 features 和 features2 包含同一批次图像的特征,即 features[i] 对于每个 i 来说都是 features2[i] 的相同图像,那么使用 numpy 函数将特征分组到单个数组中是有意义的concatenate():

newArray = np.concatenate((features, features2), axis=3)
Run Code Online (Sandbox Code Playgroud)

其中 3 是串联数组的轴。在这种情况下,您最终将得到一个维度为 (14637, 10, 10, 103) 的新数组。

但是,如果它们引用完全不同批次的图像,并且您希望将它们合并在第一个轴上,以便将 features2 的 14637 个图像放置在第一个 14637 个图像之后,那么您就不可能得到一个数组,因为 numpy 数组的结构是矩阵,而不是对象列表。

例如,如果您尝试执行:

> a = np.array([[0, 1, 2]]) // shape = (1, 3)
> b = np.array([[0, 1]]) // shape = (1, 2)
> c = np.concatenate((a, b), axis=0)
Run Code Online (Sandbox Code Playgroud)

然后,您将得到:

ValueError: all the input array dimensions except for the concatenation axis must match exactly
Run Code Online (Sandbox Code Playgroud)

因为您沿 axis = 0 连接,但轴 1 的尺寸不同。