numpy.argmin() alternative


While there are some interesting conversations about numpy.argmin() function’s performance (e.g. here, here and here), I want to stay away from making any comments. I also do not claim that my alternative is necessarily faster or slower than np.argmin() or np.argmax(), an alternative is an alternative.

>>> import numpy as np
>>> x = np.arange(127, 255, 50)
>>> x
array([127, 177, 227])
>>> x.argmin()
0
>>> x = x[::-1]
>>> x
array([227, 177, 127])
>>> x.argmin()
2
>>>
>>> # an alternative ...
...
>>> x = np.arange(127, 255, 50)
>>> n = range(len(x))
>>> n[x.tolist().index(min(x))]
0
>>> x = x[::-1]
>>> n[x.tolist().index(min(x))]
2
>>>

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s