argmax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Isaac

    #1

    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


  • Duncan Booth

    #2
    Re: argmax

    David Isaac wrote:
    [color=blue]
    > 1. Why is there no argmax built-in?
    > (This would return the index of the largest element in a sequence.)[/color]

    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).

    Comment

    • Max Erickson

      #3
      Re: argmax

      "David Isaac" <aisaac0@verizo n.net> wrote:
      [color=blue]
      > 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]
      >[/color]

      use len:

      len(iterable)-1


      max


      Comment

      • George Sakkis

        #4
        Re: argmax

        David Isaac wrote:
        [color=blue]
        > 1. Why is there no argmax built-in?
        > (This would return the index of the largest element in a sequence.)[/color]

        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.
        [color=blue]
        > 2. Is this a good argmax (as long as I know the iterable is finite)?
        > def argmax(iterable ): return max(izip( iterable, count() ))[1][/color]

        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(itera ble))[1]
        [color=blue]
        > 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?[/color]

        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

        Comment

        • Alexandre Fayolle

          #5
          Re: argmax

          Le 01-06-2006, David <aisaac0@verizo n.net> nous disait:[color=blue]
          > 1. Why is there no argmax built-in?
          > (This would return the index of the largest element in a sequence.)[/color]

          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

          Comment

          • Peter Otten

            #6
            Re: argmax

            David Isaac wrote:
            [color=blue]
            > 2. Is this a good argmax (as long as I know the iterable is finite)?
            > def argmax(iterable ): return max(izip( iterable, count() ))[1][/color]

            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:
            [color=blue][color=green][color=darkred]
            >>> from itertools import count, izip
            >>> def argmax(iterable ):[/color][/color][/color]
            .... return max(izip(iterab le, count()))[1]
            ....[color=blue][color=green][color=darkred]
            >>> class Int(int): pass[/color][/color][/color]
            ....[color=blue][color=green][color=darkred]
            >>> type(max([Int(0), 0]))[/color][/color][/color]
            <class '__main__.Int'> # must be the first item then[color=blue][color=green][color=darkred]
            >>> argmax([Int(0), 0])[/color][/color][/color]
            1

            If you care, here's the fix building on George's implementation:
            [color=blue][color=green][color=darkred]
            >>> def argmax2(iterabl e):[/color][/color][/color]
            .... return -max((v, -i) for i, v in enumerate(itera ble))[1]
            ....[color=blue][color=green][color=darkred]
            >>> argmax2([Int(0), 0])[/color][/color][/color]
            0

            Peter

            Comment

            • David Isaac

              #7
              Re: argmax

              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


              Comment

              • Steven Bethard

                #8
                Re: argmax

                David Isaac wrote:[color=blue]
                > 2. Is this a good argmax (as long as I know the iterable is finite)?
                > def argmax(iterable ): return max(izip( iterable, count() ))[1][/color]

                In Python 2.5:

                Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
                (Intel)] on win32[color=blue][color=green][color=darkred]
                >>> iterable = [5, 8, 2, 11, 6]
                >>> import operator
                >>> max(enumerate(i terable), key=operator.it emgetter(1))[/color][/color][/color]
                (3, 11)


                STeVe

                Comment

                • Ben Cartwright

                  #9
                  Re: argmax

                  David Isaac wrote:[color=blue]
                  > 2. Is this a good argmax (as long as I know the iterable is finite)?
                  > def argmax(iterable ): return max(izip( iterable, count() ))[1][/color]

                  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(s eq)). 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

                  Comment

                  Working...