Make np.argmax, np.argmin to return indexes and values at the same time
See original GitHub issueActually, np.argmax returns indexes of maximum values along some axis. On the other side, np.amax returns maximum values along some axis but if you want to get indexes of maximum values with those maximum values it is necessary to go through the array again. For example:
import numpy as np
data = np.array([[0.02079073, 0.97920927], [0.00487725, 0.99512275], [0.00849596, 0.99150404], [0.96402552, 0.03597448], [0.00711506, 0.99288494]])
indexes = np.argmax(data, axis=-1)
maximum_values = data[np.arange(data.shape[0]), indexes]
print(indexes)
print(maximum_values)
print(np.amax(data, axis=-1))
>> [1 1 1 0 1]
>> [0.97920927 0.99512275 0.99150404 0.96402552 0.99288494]
>> [0.97920927 0.99512275 0.99150404 0.96402552 0.99288494]
Even with np.arange, go through the array again consumes time (specially on huge arrays). So, np.argmax and np.argmin could return indexes and values at the same time on one look (maybe with an extra parameter in order to make compatible previous versions). For 1D just indexing would be fine, but that not work for arrays with 2 or more axis because it gets the rows elements:
import numpy as np
data = np.array([0.3, 0.7, 0.5])
indexes = np.argmax(data, axis=-1)
print(data[indexes])
>> 0.7
import numpy as np
data = np.array([[0.02079073, 0.97920927], [0.00487725, 0.99512275], [0.00849596, 0.99150404], [0.96402552, 0.03597448], [0.00711506, 0.99288494]])
indexes = np.argmax(data, axis=-1)
print(data[indexes])
>> [[0.00487725 0.99512275]
[0.00487725 0.99512275]
[0.00487725 0.99512275]
[0.02079073 0.97920927]
[0.00487725 0.99512275]]
Numpy version: ‘1.17.5’ Python version: ‘3.6.9’
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
NumPy argmin(): Get Index of the Min Value in Arrays - Datagy
In this tutorial, you'll learn how to master the NumPy argmin() function to find the index position of the minimum value in a...
Read more >How to make numpy.argmax return all occurrences of the ...
argmax says: "In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence are returned.", so you ...
Read more >numpy.argmax() in Python - GeeksforGeeks
argmax() function returns indices of the max element of the array in a particular axis. Syntax : numpy.argmax(array, axis = None, out =...
Read more >np.argmax: How to Use numpy argmax() in Python
Python NumPy argmax() function returns an array of the same shape of the given array containing the indices of the maximum elements. Finding...
Read more >numpy.argmax — NumPy v1.24 Manual
Returns the indices of the maximum values along an axis. ... then the size of axis will be 1 with the resulting array...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

If you really want to do this in a single function call, over in ufunclab I have implemented an assortment of NumPy ufuncs and gufuncs, including
min_argminandmax_argmax.Here’s your example, using
ufunclab.max_argmax:You could also do:
[ind1[ind2] for ind1, ind2 in zip(data, indexes)]