【问题标题】:How to get the minimum and maximum value of arrays inside an array?如何获取数组中数组的最小值和最大值?
【发布时间】:2023-03-18 17:12:01
【问题描述】:

我正在尝试获取列的最小值和最大值。

这是我的测试代码:

from numpy import array
import numpy as np

test = [array([[619, 502, 551],
       [623, 502, 551],
       [624, 504, 551]]),
 array([[624, 498, 531],
       [628, 502, 529]]),
 array([[619, 496, 557],
       [892, 508, 559]]),
 array([[619, 494, 561],
       [895, 506, 559],
       [902, 512, 559]]),
 array([[619, 494, 559],
       [918, 510, 567]]),
 array([[619, 493, 561],
       [931, 512, 561],
       [932, 512, 561]]),
 array([[619, 494, 561],
       [942, 510, 559]]),
 array([[619, 493, 561],
       [620, 493, 559],
       [948, 512, 561]]),
 array([[619, 494, 591],
       [752, 542, 633]]),
 array([[626, 465, 567],
       [766, 532, 633]])]

data = array(test)

我试过np.min,不同的索引,但没有成功。

我希望得到 ex 的最小值和最大值。第 2 列(或任何列)

我不能使用 for 循环遍历每个项目,因为实际数据中有很多。

我们将不胜感激任何和所有建议。谢谢。

【问题讨论】:

  • 所有行中子数组的大小是否相同?
  • 子数组的列数相同,但行数不同。话虽如此,如果一个解决方案适用于测试数据,它应该适用于我。
  • 您的代码无法在我的系统上运行,引发only 2 non-keyword arguments accepted 错误。
  • @QuangHoang 抱歉,现在应该可以了。

标签: python arrays python-3.x numpy


【解决方案1】:

IIUC,你可以做stack

np.vstack([d for d in data]).min(axis=0)

输出:

array([619, 465, 529])

【讨论】:

    【解决方案2】:

    如果列相同,您可以尝试以下方法来获取最小值/最大值。

    In [28]: test = [[[619, 502, 551],
        ...:        [624, 504, 551]],
        ...: [[624, 498, 531],
        ...:        [628, 502, 529]],
        ...: [[619, 496, 557],
        ...:        [892, 508, 559]],
        ...: [[619, 494, 561],
        ...:        [895, 506, 559],
        ...:        [902, 512, 559]],
        ...: [[619, 494, 559],
        ...:        [918, 510, 567]],
        ...: [[619, 493, 561],
        ...:        [931, 512, 561],
        ...:        [932, 512, 561]],
        ...: [[619, 494, 561],
        ...:        [942, 510, 559]],
        ...: [[619, 493, 561],
        ...:        [620, 493, 559],
        ...:        [948, 512, 561]],
        ...: [[619, 494, 591],
        ...:        [752, 542, 633]],
        ...: [[626, 465, 567],
        ...:        [766, 532, 633]]]
    
    In [29]: test1 = []
    
    In [30]: [test1.append(t) for t1 in test for t in t1]
    
    In [31]: test1
    Out[31]:
    [[619, 502, 551],
     [624, 504, 551],
     [624, 498, 531],
     [628, 502, 529],
     [619, 496, 557],
     [892, 508, 559],
     [619, 494, 561],
     [895, 506, 559],
     [902, 512, 559],
     [619, 494, 559],
     [918, 510, 567],
     [619, 493, 561],
     [931, 512, 561],
     [932, 512, 561],
     [619, 494, 561],
     [942, 510, 559],
     [619, 493, 561],
     [620, 493, 559],
     [948, 512, 561],
     [619, 494, 591],
     [752, 542, 633],
     [626, 465, 567],
     [766, 532, 633]]
    
    In [32]: np.amin(test1, None)
    Out[32]: 465
    
    In [33]: np.max(test1, None)
    Out[33]: 948
    

    【讨论】:

      【解决方案3】:

      如果您只寻找一列,您可以使用 numpy 数组的索引。 例如

      arr = np.asarray(((1,2,3),(4,5,6),(7,8,9)))
      print(arr)
      [[1 2 3]
       [4 5 6]
       [7 8 9]]
      

      通过切片,您可以将列作为子数组获取,如下例所示

      print(arr[:,0])
      [1 4 7]
      

      【讨论】:

        猜你喜欢
        • 2010-11-23
        • 2011-08-16
        • 2017-12-13
        • 1970-01-01
        • 2021-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多