argmax

D

David Isaac

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
 
D

Duncan Booth

David said:
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).
 
M

Max Erickson

David Isaac said:
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
 
G

George Sakkis

David said:
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
 
A

Alexandre Fayolle

Le 01-06-2006 said:
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).
 
P

Peter Otten

David said:
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:
.... return max(izip(iterable, count()))[1]
....
class Int(int): pass ....
type(max([Int(0), 0]))
argmax([Int(0), 0])
1

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

Peter
 
D

David Isaac

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
 
S

Steven Bethard

David said:
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
 
B

Ben Cartwright

David said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,283
Latest member
SherriP988

Latest Threads

Top