473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

argmax

1. Why is there no argmax built-in?
(This would return the index of the largest element in a sequence.)

2. Is this a good argmax (as long as I know the iterable is finite)?
def argmax(iterable): return max(izip( iterable, count() ))[1]

3. If this is the only place in a module where I need count and izip,
should I import them at the module level or at the level of the function?
What are the considerations here?

Thanks,
Alan Isaac
Jun 1 '06 #1
8 7170
David Isaac wrote:
1. Why is there no argmax built-in?
(This would return the index of the largest element in a sequence.)


Probably there isn't a built-in because it isn't a commonly needed
function.

What is your use-case for argmax? If for example you want to repeatedly
remove the largest element from a list, then sort the list and pop the last
element (or use a heap, except heapq lets you pop the smallest so you can't
use it directly).
Jun 1 '06 #2
"David Isaac" <ai*****@verizon.net> wrote:
1. Why is there no argmax built-in?
(This would return the index of the largest element in a
sequence.)

2. Is this a good argmax (as long as I know the iterable is
finite)? def argmax(iterable): return max(izip( iterable, count()
))[1]


use len:

len(iterable)-1
max
Jun 1 '06 #3
David Isaac wrote:
1. Why is there no argmax built-in?
(This would return the index of the largest element in a sequence.)
I guess because it's not used frequently enough. I've needed
argmax/argmin more than once though, so I would welcome them as
builtins.
2. Is this a good argmax (as long as I know the iterable is finite)?
def argmax(iterable): return max(izip( iterable, count() ))[1]
Yes, it's ok. Here's another one that doesn't require importing
itertools:
def argmax(iterable): return max((x,i) for i,x in
enumerate(iterable))[1]
3. If this is the only place in a module where I need count and izip,
should I import them at the module level or at the level of the function?
What are the considerations here?


Both have their merits. I like having the imports close to the point
they're used, at least if used only once; OTOH having all imports at
the top of the module makes easier to see the module's dependencies
without grep'ing for import (that's especially useful for non-standard
imported modules or new additions ot the std lib if backwards
compatibility is an issue).

George

Jun 1 '06 #4
Le 01-06-2006, David <ai*****@verizon.net> nous disait:
1. Why is there no argmax built-in?
(This would return the index of the largest element in a sequence.)


You'll get argmin and argmax in Numeric and its descendants (numarray
and numpy).
--
Alexandre Fayolle LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
Python et calcul scientifique: http://www.logilab.fr/science
Jun 1 '06 #5
David Isaac wrote:
2. Is this a good argmax (as long as I know the iterable is finite)?
def argmax(iterable): return max(izip( iterable, count() ))[1]


There's a subtle difference to the builtin: argmax() gives you the (index of
the) last maximum while max() returns the (value of the) first maximum:
from itertools import count, izip
def argmax(iterable): .... return max(izip(iterable, count()))[1]
.... class Int(int): pass .... type(max([Int(0), 0])) <class '__main__.Int'> # must be the first item then argmax([Int(0), 0]) 1

If you care, here's the fix building on George's implementation:
def argmax2(iterable): .... return -max((v, -i) for i, v in enumerate(iterable))[1]
.... argmax2([Int(0), 0])

0

Peter
Jun 1 '06 #6
Thanks for all the replies.
A couple of comments.

1. I think the usefulness of an argmax built-in can be assessed
by looking at other languages (and e.g. at numpy). So I do not
buy the "not needed" argument as presented. More like "haven't
got around to it," I'm thinking.

2. The particular use case this time is strategy choice.
The desired strategy (i.e., index) is the one with the highest payoff.

3. Thanks to George, and to Peter for noticing a subtle difference
in the implementations.

Alan Isaac
Jun 1 '06 #7
David Isaac wrote:
2. Is this a good argmax (as long as I know the iterable is finite)?
def argmax(iterable): return max(izip( iterable, count() ))[1]


In Python 2.5:

Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
(Intel)] on win32
iterable = [5, 8, 2, 11, 6]
import operator
max(enumerate(iterable), key=operator.itemgetter(1))

(3, 11)
STeVe
Jun 1 '06 #8
David Isaac wrote:
2. Is this a good argmax (as long as I know the iterable is finite)?
def argmax(iterable): return max(izip( iterable, count() ))[1]


Other than the subtle difference that Peter Otten pointed out, that's a
good method.

However if the iterable is a list, it's cleaner (and more efficient) to
use seq.index(max(seq)). That way you won't be creating and comparing
all those tuples.

def argmax(it):
try:
it.index
except AttributeError:
it = list(it)
# Or if it would too expensive to convert it to list:
#return -max((v, -i) for i, v in enumerate(it))[1]
return it.index(max(it))

--Ben

Jun 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
32
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in...
2
by: drife | last post by:
Hello, I am looking for suggestions for how I might optimize my code (make it execute faster), and make it more streamlined/ elegent. But before describing my code, I want to state that I am...
1
by: cnb | last post by:
, 'argmax', 'argmin', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 'diagonal', 'dtype', 'dump',...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.