0

I have 3 numpy arrays of shape (224, 224, 20). I want to go through each of (224, 224) values in all 20 layers (dimensions) and compare them to get the highest among them. For 3 Dimensional, I am able to come up with this:

arr1 = np.array([[[1,2,3],[4,5,6]],[[10,11,12],[15,16,17]]])
for x in range(0,2):                                                                                                            
       for y in range(0,2):                                                                                                            
             print(arr1[:,x,y])

But, I somehow couldn't understand how to convert it for (224,224,20) shaped arrays. I also need the index of the layer which contains the maximum value.

Sree
  • 973
  • 2
  • 14
  • 32

2 Answers2

2

To get max values along one dimension, you can use numpy.amax, checkout:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.amax.html

Nothing More
  • 873
  • 12
  • 29
1

You can do this with numpy.max instead of a for loop:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.max.html

np.max(arr1, axis=2)

To get the index, use numpy.argmax

https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html

np.argmax(arr1, axis=2)
Simon Crane
  • 2,122
  • 2
  • 10
  • 21
  • Got it, but what if I need to know the index of the layer with the maximum value? – Sree Oct 02 '19 at 08:16
  • I am just curious, is there any difference between numpy.max and numpy.amax. All i see on internet is numpy.max is just an alias of numpy.amax. But, is there any difference either in terms of performance or anyother factors. – Sree Oct 02 '19 at 08:29
  • 1
    https://stackoverflow.com/questions/33569668/numpy-max-vs-amax-vs-maximum – Simon Crane Oct 02 '19 at 08:32