【问题标题】:Given that i have a 2d array and i want to reshape it to 1d with one value per row鉴于我有一个 2d 数组,我想将其重塑为 1d,每行一个值
【发布时间】:2020-10-20 23:01:48
【问题描述】:

这是我的数组

arr = np.array([[0, 1],
[3, 4],
[6, 7]])

flat_arr = np.reshape(arr, -1)

我得到以下结果:

[0 1 2 3 4 5 6 7 8]

我想要的结果是:

[0]
[1]
[3]
[4]
[5]...

【问题讨论】:

  • arr.reshape(-1,1)

标签: python numpy reshape


【解决方案1】:

有几种方法可以做到:

flat_arr[:, None]
flat_arr[:, np.newaxis]
np.expand_dims(flat_arr, axis=1)

另外,你可以像这样重塑它:

arr.reshape(-1, 1)

【讨论】:

    【解决方案2】:

    您可以使用这个新形状:

    import numpy as np
    
    arr = np.array([[0, 1], [3, 4], [6, 7]])
    
    flat_arr = np.reshape(arr, (arr.shape[0] * arr.shape[1], 1))
    
    print(flat_arr)
    

    输出:

    [[0]
     [1]
     [3]
     [4]
     [6]
     [7]]
    

    此外,正如@MarkMeyer 所添加的,您可以使用:

    flat_arr = np.reshape(arr, (-1, 1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2022-01-21
      • 2022-11-03
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多