Quickest way to find the nth largest value in a numpy Matrix

前端 未结 6 1899
孤独总比滥情好
孤独总比滥情好 2020-12-29 06:56

There are lots of solutions to do this for a single array, but what about a matrix, such as:

>>> k
array([[ 35,  48,  63],
       [ 60,  77,  96],
          


        
6条回答
  •  天命终不由人
    2020-12-29 07:33

    As said, np.partition should be faster (at most O(n) running time):

    np.partition(k.flatten(), -2)[-2]
    

    should return the 2nd largest element. (partition guarantees that the numbered element is in position, all elements before are smaller, and all behind are bigger).

提交回复
热议问题