0

What is the best/shortest way to get the TWO columns of with max value after selecting the THREE max in every specified row

  [[0.  , 0.  , 0.  ],
   [0.19, 0.  , 0.  ],
   [0.  , 0.29, 0.  ],
   [0.42, 0.  , 0.13],
   [0.12, 0.12, 0.13],
   [0.13, 0.1 , 0.26],
   [0.  , 0.  , 0.  ],
   [0.  , 0.12, 0.  ],
   [0.25, 0.  , 0.48],
   [0.  , 0.  , 0.21]])

so the three max vals at rows 3,4,5 are

    In [132]: np.max(ary[[3,4,5],:],axis=1)                                                                                                                                    
    Out[132]: array([0.42, 0.13, 0.26])

now i have to select the columns of the two max values:

  In [133]: np.argmax(ary[[3,4,5],:],axis=1)                                                                                                                                 
  Out[133]: array([0, 2, 2])

in this case that is element[0]=0 and element[2]=2, ignoring element[1]=2

Is there a quicker way of getting the col-ixs of max-of-max ?

there doesnt seem to be direct argmax-max function you have to always do max+argmax (store intermediary result) and do argmax again


is this correct :

 np.argsort(np.max(ary[[3,4,5],:],axis=1))[::-1][:2]
sten
  • 7,028
  • 9
  • 41
  • 63
  • I am not sure your solution always works: what the result is supposed to look like if the maximum values are all found in the last column? Besides this, is your actual input bigger than this or you do this operation many times? – Jérôme Richard May 01 '21 at 20:23
  • good catch.. in reality it will be something like 10 col out of 1000, so it wont have the "same column" problem – sten May 01 '21 at 20:55

0 Answers0