【问题标题】:Append dimensional numpy array附加维度 numpy 数组
【发布时间】: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


【解决方案1】:

使用np.stack

def dim0_stack(*arrays):
    return np.stack(arrays, axis = 0)

【讨论】:

    【解决方案2】:

    另一种方法:

    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,所以我想向您展示如何使用它。

    【讨论】:

      【解决方案3】:

      另一种更字面的方法

      result = np.zeros((2, A.shape[0], A.shape[1], A.shape[2]))
      result[0, :, :, :] = A
      result[1, :, :, :] = B
      

      【讨论】:

        猜你喜欢
        • 2015-01-29
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        • 2017-06-10
        • 2016-05-29
        • 1970-01-01
        相关资源
        最近更新 更多