【问题标题】:how to get elements in sub-array of a numpy array in python如何在python中获取numpy数组的子数组中的元素
【发布时间】:2022-01-19 09:40:33
【问题描述】:

我在 python 中读取 numpy 数组的子数组的条目时遇到了麻烦。我有这样的东西:

a = np.array([ [453,254,[1,2,3,4,5]], [743,251,[10,20,30,40,50]], [127,393,[11,22,33,44,55]] ], dtype=object)

我需要计算第二个位置子数组的每n列的平均值,即np.mean([1,10,11]), np.mean([2,20,22])

如何获取[1,10,11], [2,20,22]等子数组?

我尝试了 ":""," 的不同组合,但我无法弄清楚。我还将dtype=object 放在numpy 数组定义中,但没有任何区别。提前致谢。

【问题讨论】:

  • np.array(a[:,2].tolist()).mean(1)?从长远来看,也许将数组一分为二会更方便。

标签: python numpy sub-array


【解决方案1】:

你可以这样做:

np.mean([x[0] for x in a[:,2]])
np.mean([x[1] for x in a[:,2]])
...

【讨论】:

    【解决方案2】:

    我找到了一个可能的解决方案

    np.dstack((a[:,2]))[0][0]
    

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 2021-07-10
      • 1970-01-01
      • 2012-02-27
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      相关资源
      最近更新 更多