1

so my questions is pretty straight forward. I have a 3-d array and would like to get the maximal value alone the first two axes.

import numpy as np
array = np.zeros((3,3,2))
array[1][1][0] = 1
array[1][1][1] = 2

How do I now check along [1][1] which value has the max value? I would suspect to use np.argmax() but I googled for ages and could not find a working solution. Thank you in advance!

To be clear, I want my return to be 2, just a single integer.

Ok iDoki
  • 79
  • 2
  • 10
  • are you looking to find the max value for just this case or any slice in general, like `array[i][j]`? – C.Nivs Dec 12 '18 at 23:37
  • Your expected output of `2` is the maximum *value*, not the *argument of the maximum* ("argmax") value. Do you actually expect to get the coordinates along the first two dimensions *of* the maximum value, which would be `(1, 1)`? – Peter Leimbigler Dec 12 '18 at 23:38
  • Actually, re-reading your question, if you are looking for the argument in the last axis that points to the maximum value, you can use `array[1,1,:].argmax()` – Peter Leimbigler Dec 12 '18 at 23:42
  • Sorry I was working. I expect to find the max value for any value in the array, like the top comment said array[i][j]. I want to compare all values for the z axis and return the one that has the biggest value. In the example I want as an input the first two axes [i][j] and then check for all possible values that the third axes can be [z]. In the example: [1][1][0] and [1][1][1] and since the second has a higher value I want that as return – Ok iDoki Dec 13 '18 at 17:11

1 Answers1

1

For what you describe, you could just do:

array[1,1].max()

which will return 2. If you instead wanted the maximum value along the last axis for every combination of the first two axes, you would do:

array.max(axis=-1)

which in your case will return a 3x3 array of the maximum values along the last axis:

[[0. 0. 0.]
 [0. 2. 0.]
 [0. 0. 0.]]

If you want the indices of the maximum value, you would instead use argmax, just like you would max above:

array[1,1].argmax()

which in this case returns just 1. You would then have to append that to (1,1) to get the complete index to the maximum value in your original array (ie (1,1,1)).

tel
  • 13,005
  • 2
  • 44
  • 62