0

I am trying to find the max-indices of a 3d array over (0, 1) axis.

I tried using argwhere with amax, but it returns multiple indices over the axis if it have multiple max values

x = [[[ 4  5  9]
      [ 0 13  6]]

     [[12 11 13]
      [ 5 13  8]]]  

np.amax(x, axis=(0,1))
>> [12 13 13]  #I just want the indices of the following max values

x==np.amax(x, axis=(0,1))
#this seems to be the problem, multiple True in the second column
             v
>>[[[False False False]
    [False  True False]]

    [[True False  True]
     [False  True False]]]

np.argwhere(x==np.amax(x, axis=(0,1))) #this should return 3 indices instead of 4
>>[[0 1 1]
   [1 0 0]
   [1 0 2]
   [1 1 1]]

So is there any 'numpy way' to get unique max-indices of a 3d array over (0, 1) axis.

Julkar9
  • 1,508
  • 1
  • 12
  • 24
  • Just an fyi: there is an outstanding issue for argmax to allow multiple axis: https://github.com/numpy/numpy/issues/6296 . – 9769953 Feb 08 '21 at 10:22
  • https://stackoverflow.com/questions/30589211/numpy-argmax-over-multiple-axes-without-loop may help. I'm not (yet) flagging it as a duplicate, because the actual question there is perhaps more convoluted than just "argmax over multiple axes". – 9769953 Feb 08 '21 at 10:24
  • You could use your boolean array, and set all values in the unrelated dimension(s) to `False`. – 9769953 Feb 08 '21 at 10:25
  • Note that with two values of 13 along the second axis (axis == 1), it is perhaps not surprising you get 4 instead of 3 indices: one for 12, two for 13, and then one for 13, for each value of the third axis. – 9769953 Feb 08 '21 at 10:32
  • @00 I actually thought about manually tampering the Boolean array, even a better approach would be to take unique values along the z axis (3rd column) in the output array, but wanted to know whether a more efficient and clean approach is available or not considering this function will be called quite a few million times – Julkar9 Feb 08 '21 at 10:57
  • @00 I do realize why I am getting 4 coordinates, "this seems to be the problem, multiple True in the second column", also thanks for your help – Julkar9 Feb 08 '21 at 10:58
  • @00 the linked thread seems to be dealing with argmax over (1,2) axis, I am not sure how can I change this to (0, 1) efficiently – Julkar9 Feb 08 '21 at 11:41
  • My point is more that you have to ask whether there is really a proper result to asking for something like `np.argmax(x, axis=(0, 1))` (which doesn't work, of course), with those multiple 13 values. It's not that you don't realise you get four indices, but whether there is an actual answer to your "maximum" question. For example, for a 1D `[1,2,3,2,1,3]` array, what should argmax be? 2, 5, or both? (NumPy here chooses the first, but one could argue for `[2, 5]` as well). – 9769953 Feb 08 '21 at 12:05
  • Hm, if you call that function a few million times (assuming this is not over a timespan of days or years), it almost sounds as if there is another optimisation to be made (by further vectorization). Possibly not, without knowing the actual code, and that would make this a whole other question anyway. Of course, if this part starts to be the really limiting part in a million-times loop, it may be your worth to use Cython, Numba or C. – 9769953 Feb 08 '21 at 12:08
  • I guess I will just manually remove the duplicates, on a side note it will be used in maxpool backpropagation. Also thank you for your help. – Julkar9 Feb 08 '21 at 14:26

0 Answers0