Web Development Labs
Web Development Labs

Reputation: 431

How does np.argmax(axis=0) work on 3D arrays?

I am stuck at, as to how does np.argmax(arr, axis=0) work? I know how np.argmax(axis=0) works on 2D arrays. But this 3D one has really confused me.

My Code:

   arr = np.array([[[ 1,  2,  3],
                   [ 4,  5,  6],
                   [ 7,  8,  9],
                   [10, 11, 12]],
                   
                   [[13, 14, 15],
                   [16, 17, 18],
                   [19, 20, 21],
                   [22, 23, 24]],
                   
                   [[25, 26, 27],
                   [28, 29, 30],
                   [31, 32, 33],
                   [34, 35, 36]]])

Operation:

np.argmax(arr, axis = 0)

Output:

array([[2, 2, 2],
       [2, 2, 2],
       [2, 2, 2],
       [2, 2, 2]], dtype=int64)

FYI - I do know how np.argmax(axis=0) works on 2D arrays. But this 3D one has really confused me.

Upvotes: 0

Views: 181

Answers (1)

mathfux
mathfux

Reputation: 5949

You need to understand better what is axis=0 here. It can be interpreted as height level of rectangle. So your output shows different levels of that rectangle:

   level 0       level 1       level 2
[ 1,  2,  3]  [13, 14, 15]  [16, 17, 18]
[ 4,  5,  6]  [16, 17, 18]  [19, 20, 21]
[ 7,  8,  9]  [19, 20, 21]  [22, 23, 24]
[10, 11, 12]  [22, 23, 24]  [25, 16, 27]

Then argmax describes indices of levels at which max values are attained. They are:

[16, 17, 18]
[19, 20, 21]
[22, 23, 24]
[25, 16, 27]

It's definitely the upmost level (number 2) for any of these cells so argmax of every cell is assigned to 2.

Upvotes: 2

Related Questions