0

I have a 3d numpy array of shape (150, 9, 5) and I would like to get the argmax of the 3rd dimension (out of the elements) but I cannot get it done. For example, let's say the array looks like (this example does not follow the shape I am using in my code):

[[[615, 142, 18]],

   [[12, 412, 98]],

   [[5, 11, 162]],

   [[19, 76, 7]],

   [[56, 78, 12]],

   [[119, 41, 55]],

   [[26, 7, 15]],

   [[19, 67, 53]]]

I would like to get back an array of shape (150, 9, 1) (again this is in my case, not related to the example). For the example it would be:

[[[0]],

   [[1]],

   [[2]],

   [[1]],

   [[1]],

   [[0]],

   [[0]],

   [[1]]]

When I try np.argmax() with both axis 0 and 1, I get wrong results.

Is there a way to work it out directly or should I go through each (9,5) using a for loop ?

Wazaki
  • 899
  • 1
  • 8
  • 22

1 Answers1

0

You've got extra axes with extra dimensions:

import numpy as np
a=np.array([[[615, 142, 18]],

   [[12, 412, 98]],

   [[5, 11, 162]],

   [[19, 76, 7]],

   [[56, 78, 12]],

   [[119, 41, 55]],

   [[26, 7, 15]],

   [[19, 67, 53]]])

a.argmax(axis=2)

Output:

Out[82]: 
array([[0],
       [1],
       [2],
       [1],
       [1],
       [0],
       [0],
       [1]], dtype=int64)
Juan C
  • 5,846
  • 2
  • 17
  • 51