在python中查找具有5列的数组的第二个最大值

在python中查找具有5列的数组的第二个最大值,python,numpy,Python,Numpy,我正在使用以下代码搜索最大值pred\u flat。有没有直接的方法找到第二个最大值 line_max_flat = np.max(pred_flat, axis=1) ##creates an array with 2500 entries, each containing the max of the row 变量pred_flat是一个大小为(2500,5)的数组,其他问题与第二个最大值有关,只有一列或一个列表的地址数组 编辑: 输入的一个例子是: pred_flat=[[0.1,0.

我正在使用以下代码搜索最大值
pred\u flat
。有没有直接的方法找到第二个最大值

line_max_flat = np.max(pred_flat, axis=1)  ##creates an array with 2500 entries, each containing the max of the row
变量
pred_flat
是一个大小为(2500,5)的数组,其他问题与第二个最大值有关,只有一列或一个列表的地址数组

编辑: 输入的一个例子是:

pred_flat=[[0.1,0.2,0.3,0.5,0.7]
           [0.5.0.4,0.9,0.7,0.3]
           [0.9,0.7,0.8,0.4,0.1]]
输出应为:

line_max_flat=[0.5,0.7,0.8]

我们可以使用heapq模块的nlargest方法返回一个iterable的前n个最大数的列表,虽然它不是完全直接的,但它是一个足够简单的代码,可以工作

import numpy as np
import heapq

pred_flat = np.array([[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[5, 6, 7, 8, 9]]) # example used in my code

line_max_flat = []
for i in pred_flat:
    # assuming unique elements, otherwise use set(i) below
    _, sec_max = heapq.nlargest(2, i) # returns a list [max, second_max] - here n=2 
    line_max_flat.append(sec_max)

line_max_flat = np.array(line_max_flat) # make array
print(line_max_flat)
输出:

[4 8 8] # which is the expected result from my example array

第二个最大值是最小值(
np.min
),因为每行有2个值……对不起,我是说5!打字错误我要马上纠正它@标题正确吗?请创建定义输入的含义,以及该输入的预期输出。在大小为10或更小的小型阵列上执行此操作。在上面的示例数组中,每一行都已排序,但不需要排序。那只是巧合