【问题标题】:how to merge different dimensions arrays in python?如何在python中合并不同维度的数组?
【发布时间】:2019-02-26 04:29:24
【问题描述】:

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

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

【问题讨论】:

  • 您希望新数组在串联后是什么?
  • 您是在 Keras 还是 Numpy 中寻找答案?你想要一个融合两者的神经网络层吗?如果是这样,那么您可以使用 Keras concatenate
  • @caseWestern 连接不仅用于合并图层...??我正在做早期融合的事情。 neb 的回答非常适合我。谢谢
  • concatenate 在 Keras 中的作用与在 Numpy 中的 concatenate 相同,只是使用 Keras 张量而不是 Numpy 数组。因此,如果您需要在神经网络中合并两个张量,您将使用 Keras 版本。不过,我很高兴该解决方案对您有用。
  • 哦,好吧,我不知道。谢谢你的信息:)

标签: python image-processing deep-learning


【解决方案1】:

我认为如果你使用类会更好。

class your_class:
     array_1 = []
     array_2 = []

 final_array = []

 for x in range(len(your_previous_one_array)):
     temp_class = your_class
     temp_class.array_1 = your_previous_one_array
     temp_class.array_2 = your_previous_two_array
     final_array.append(temp_class)

【讨论】:

    【解决方案2】:

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

    newArray = np.concatenate((features, features2), axis=3)
    

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

    但是,如果它们指的是完全不同的图像批次,并且您希望将它们合并到第一个轴上,以便将特征 2 的 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)
    

    然后,你会得到:

    ValueError: all the input array dimensions except for the concatenation axis must match exactly
    

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

    【讨论】:

    • 感谢@neb,它对我有用。是的,功能来自同一批次。我完全想要这个结果(14637、10、10、103)。 :) 谢谢
    • @FarzanM.Ñóòrî 我的荣幸 :)
    【解决方案3】:

    如果处理 numpy 数组,您应该能够使用 concatenate 方法并指定数据应该合并的轴。基本上:np.concatenate((array_a, array_b), axis=2)

    【讨论】:

      猜你喜欢
      • 2019-09-19
      • 2022-12-13
      • 2014-03-27
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2019-04-19
      • 2021-08-19
      相关资源
      最近更新 更多