【问题标题】:Transform ndarray shape变换ndarray形状
【发布时间】:2017-05-14 22:12:36
【问题描述】:

我正在使用 python / Numpy 将小图像存储到 ndarray 中。 当我试图将 ndarray 从 32,32,1 形状转换为 1,32,32,1 时,我被卡住了。有什么帮助吗?谢谢

【问题讨论】:

  • 嘿,您对其中一个答案满意吗?

标签: python numpy multidimensional-array reshape


【解决方案1】:

您需要扩展 numpy 数组的维度。使用np.expand_dims

arr = np.expand_dims(arr, axis=0)

【讨论】:

    【解决方案2】:

    arr[np.newaxis, :, :, :] 可以工作

    【讨论】:

      【解决方案3】:

      除了显式添加轴,您还可以显式重塑它以添加轴:

      >>> import numpy as np
      >>> arr = np.ones((32, 32, 1))  # just ones for demonstration purposes
      
      >>> reshaped = arr.reshape(1, *arr.shape)
      >>> reshaped.shape
      (1, 32, 32, 1)
      

      【讨论】:

        猜你喜欢
        • 2020-09-23
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多