Ask Your Question
1

How can the argmax operation be performed quickly in numpy?

asked 2021-12-20 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-01-09 19:00:00 +0000

qstack gravatar image

The argmax operation can be performed quickly in numpy using the argmax function. The argmax function returns the indices of the maximum values along an axis.

Here is an example:

import numpy as np a = np.array([[5, 2, 3], [7, 1, 4], [0, 8, 6]]) # Find the index of the maximum value along axis 0 max_idx = np.argmax(a, axis=0) print(max_idx) # Output: [1 2 2] # Find the index of the maximum value along axis 1 max_idx = np.argmax(a, axis=1) print(max_idx) # Output: [0 0 1] 

In this example, we have a 3x3 numpy array a and we are finding the index of the maximum value along axis 0 and axis 1 using the argmax function. We get the output [1 2 2] when we find the maximum value along axis 0, which means that the maximum values are located at index 1 in the first column, index 2 in the second column, and index 2 in the third column. Similarly, we get the output [0 0 1] when we find the maximum value along axis 1, which means that the maximum values are located at index 0 in the first row, index 0 in the second row, and index 1 in the third row.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-20 11:00:00 +0000

Seen: 13 times

Last updated: Jan 09 '23