question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make np.argmax, np.argmin to return indexes and values at the same time

See original GitHub issue

Actually, 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:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
WarrenWeckessercommented, Feb 21, 2020

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_argmin and max_argmax.

Here’s your example, using ufunclab.max_argmax:

In [21]: import numpy as np                                                     

In [22]: import ufunclab                                                        

In [23]: data = np.array([[0.02079073, 0.97920927], [0.00487725, 0.99512275], [0
    ...: .00849596, 0.99150404], [0.96402552, 0.03597448], [0.00711506, 0.992884
    ...: 94]])                                                                  

In [24]: data                                                                   
Out[24]: 
array([[0.02079073, 0.97920927],
       [0.00487725, 0.99512275],
       [0.00849596, 0.99150404],
       [0.96402552, 0.03597448],
       [0.00711506, 0.99288494]])

In [25]: values, indices = ufunclab.max_argmax(data, axis=-1)                   

In [26]: values                                                                 
Out[26]: array([0.97920927, 0.99512275, 0.99150404, 0.96402552, 0.99288494])

In [27]: indices                                                                
Out[27]: array([1, 1, 1, 0, 1])
1reaction
gmodacommented, Dec 11, 2020

You could also do:

[ind1[ind2] for ind1, ind2 in zip(data, indexes)]

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found