How to sort in descending order with numpy?

后端 未结 1 723
忘掉有多难
忘掉有多难 2021-02-13 12:09

I have a numpy array like this:

A = array([[1, 3, 2, 7],
           [2, 4, 1, 3],
           [6, 1, 2, 3]])

I would like to sort the rows of th

1条回答
  •  孤街浪徒
    2021-02-13 12:49

    Just multiply your matrix by -1 to reverse order:

    [In]: A = np.array([[1, 3, 2, 7],
                        [2, 4, 1, 3],
                        [6, 1, 2, 3]])
    [In]: print( np.argsort(-A) )
    [Out]: [[3 1 2 0]
            [1 3 0 2]
            [0 3 2 1]]
    

    0 讨论(0)
提交回复
热议问题