What does it mean to sort an array / matrix using argmax as a key?

I am trying to understand the mechanism of a function around a sorting topic in numpy.

    import numpy as np
    arr = [[8, 5, 9], 
            [3, 9.5, 5], [5.5, 4, 3.5], [6, 2, 1],
            [6,1,2],[3,2,1],[8,5,3]]
    res = sorted(arr, key=np.argmax)

This gives me the following result:

    print(res)
    [[5.5, 4, 3.5], [6, 2, 1], [6, 1, 2],
      [3, 2, 1], [8, 5, 3], [3, 9.5, 5], [8, 5, 9]]  

I am an R user and not very familiar with Python. I may have some idea of ​​the role of the key argument, but for this example, I ask you for help. In the simple case, if the argument keyis defined as a function that returns the first element, it sortedsorts the array based on its first element, but I do not see how this works with argmax. Thank,

+4
source share
2 answers

argmax . .

:

print([np.argmax(x) for x in arr])

:

[2, 1, 0, 0, 0, 0, 0]

. , , , .

, "" , , , (: , . Bakuriu)

+2

np.argmax . 3 ,

>>> np.argmax([8,5,3])
0
>>> np.argmax([1,2,3])
2
0
source

Source: https://habr.com/ru/post/1651770/


All Articles