【问题标题】:Take axis where third axis has max value取第三个轴具有最大值的轴
【发布时间】:2023-01-27 21:47:36
【问题描述】:

我有一组 n 堆叠二维矩阵,这里是 n = 3

[[[-1,  90],
  [-2,  50],
  [-3,  10]],

 [[-4,  99],
  [-5,  40],
  [-6,  5]],

 [[-7, 0],
  [-8, 0],
  [-9, 60]]])

我想返回一个二维矩阵,其行是 n 堆叠矩阵的第二列具有最大值的行。

对于上面的数组,预期的输出是:

[[-4, 99],
 [-2, 50],
 [-9, 60]]

我尝试使用内置的np.max,但这将返回两个轴上的最大值,即:

[[-1, 99],
 [-2, 50],
 [-3, 60]]

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    利用:

    a[np.argmax(a[..., 1].max(axis=1))]
    

    输出:

    array([[-4, 99],
           [-5, 40],
           [-6,  5]])
    

    中间步骤:

    # get last column
    a[..., 1]
    
    array([[90, 50, 10],
           [99, 40,  5],
           [ 0,  0, 60]])
    
    # max of last column
    a[..., 1].max(axis=1)
    
    array([90, 99, 60])
    
    # index of max value of max of last column
    np.argmax(a[..., 1].max(axis=1))
    
    1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多