Ad

Get The Value At A Position From All Layers In Python

- 1 answer

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.

Ad

Answer

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)
Ad
source: stackoverflow.com
Ad