computing a weighted sum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andreif@mail.dntis.ro

    #1

    computing a weighted sum

    Suppose I have a list of n floats x and a list of n floats w and I want
    to compute x[0]*w[0] + .. + x[n-1]*w[n-1].

    Is there some elegant expression (perhaps using lambda) to have it done
    in one statement ? As in :
    y = lambda x,w : ...

    I ask because the way I am doing it now :
    y = 0
    for i in range(0,n): y += x[i]*w[i]

    doesn't seem very pythonic :)

    Thanks,
    Andrei

  • Will McGugan

    #2
    Re: computing a weighted sum

    andreif@mail.dn tis.ro wrote:[color=blue]
    > Suppose I have a list of n floats x and a list of n floats w and I want
    > to compute x[0]*w[0] + .. + x[n-1]*w[n-1].
    >
    > Is there some elegant expression (perhaps using lambda) to have it done
    > in one statement ? As in :
    > y = lambda x,w : ...
    >
    > I ask because the way I am doing it now :
    > y = 0
    > for i in range(0,n): y += x[i]*w[i]
    >
    > doesn't seem very pythonic :)
    >[/color]
    I'll take a stab at that!

    In Python 2.3

    sum( [ _x * _w for _x, _w in zip( x, w ) ] )

    or in 2.4

    sum( _x * _w for _x, _w in zip( x, w ) )

    You may want to use itertools.izip in place of zip if the lists are large.

    Will McGugan

    Comment

    • Duncan Booth

      #3
      Re: computing a weighted sum

      wrote:
      [color=blue]
      > Suppose I have a list of n floats x and a list of n floats w and I want
      > to compute x[0]*w[0] + .. + x[n-1]*w[n-1].
      >
      > Is there some elegant expression (perhaps using lambda) to have it done
      > in one statement ? As in :
      > y = lambda x,w : ...[/color]
      [color=blue][color=green][color=darkred]
      >>> x = [1, 2, 3]
      >>> w = [4, 5, 6]
      >>> sum(a*b for (a,b) in zip(x, w))[/color][/color][/color]
      32

      Comment

      • andreif@mail.dntis.ro

        #4
        Re: computing a weighted sum

        Thanks Will, the 2.4 expression looks really nice.

        Comment

        • Christos TZOTZIOY Georgiou

          #5
          Re: computing a weighted sum

          On 16 Mar 2005 06:49:09 -0800, rumours say that andreif@mail.dn tis.ro might have
          written:
          [color=blue]
          >Suppose I have a list of n floats x and a list of n floats w and I want
          >to compute x[0]*w[0] + .. + x[n-1]*w[n-1].
          >
          >Is there some elegant expression (perhaps using lambda) to have it done
          >in one statement ? As in :
          > y = lambda x,w : ...
          >
          >I ask because the way I am doing it now :
          > y = 0
          > for i in range(0,n): y += x[i]*w[i]
          >
          >doesn't seem very pythonic :)[/color]

          Your method seems to be the one closest to what's generally considered as
          pythonic.

          Anyway, a functional equivalent:

          ..>> from itertools import starmap, izip
          ..>> import operator
          ..>> x= [1,2,3,4]
          ..>> w=[3.0, 6.0, 9.0, 12.0]
          ..>> sum(starmap(ope rator.mul, izip(x,w)))
          90.0
          ..>>
          --
          TZOTZIOY, I speak England very best.
          "Be strict when sending and tolerant when receiving." (from RFC1958)
          I really should keep that in mind when talking with people, actually...

          Comment

          • Steven Bethard

            #6
            Re: computing a weighted sum

            Will McGugan wrote:[color=blue]
            > In Python 2.3
            >
            > sum( [ _x * _w for _x, _w in zip( x, w ) ] )
            >
            > or in 2.4
            >
            > sum( _x * _w for _x, _w in zip( x, w ) )[/color]

            Any reason for the leading underscores? If you're trying to avoid
            polluting your namespace, you should note that generator expressions
            don't leak their loop variables, so you can write the second as:

            sum(x*w for x, w in zip(x, w))

            without any worries of overwriting x and w. (Of course I would probably
            name them something different anyway, e.g. x_item and w_item...)

            STeVe

            Comment

            • andreif@mail.dntis.ro

              #7
              Re: computing a weighted sum

              Even if language permits
              sum(x*w for x, w in zip(x, w))
              would seem confusing for anyone watching the code

              Maybe
              sum(xi*wi for xi, wi in zip(x, w))
              would be more appropiate

              Andrei

              Comment

              • Fernando Perez

                #8
                Re: computing a weighted sum

                andreif@mail.dn tis.ro wrote:
                [color=blue]
                > Suppose I have a list of n floats x and a list of n floats w and I want
                > to compute x[0]*w[0] + .. + x[n-1]*w[n-1].
                >
                > Is there some elegant expression (perhaps using lambda) to have it done
                > in one statement ? As in :
                > y = lambda x,w : ...
                >
                > I ask because the way I am doing it now :
                > y = 0
                > for i in range(0,n): y += x[i]*w[i]
                >
                > doesn't seem very pythonic :)
                >
                > Thanks,
                > Andrei
                >[/color]

                import Numeric
                print Numeric.dot(x,w )

                Best,

                f

                Comment

                • John Machin

                  #9
                  Re: computing a weighted sum


                  Fernando Perez wrote:[color=blue]
                  > andreif@mail.dn tis.ro wrote:
                  >[color=green]
                  > > Suppose I have a list of n floats x and a list of n floats w and I[/color][/color]
                  want[color=blue][color=green]
                  > > to compute x[0]*w[0] + .. + x[n-1]*w[n-1].
                  > >
                  > > Is there some elegant expression (perhaps using lambda) to have it[/color][/color]
                  done[color=blue][color=green]
                  > > in one statement ? As in :
                  > > y = lambda x,w : ...
                  > >
                  > > I ask because the way I am doing it now :
                  > > y = 0
                  > > for i in range(0,n): y += x[i]*w[i]
                  > >
                  > > doesn't seem very pythonic :)
                  > >
                  > > Thanks,
                  > > Andrei
                  > >[/color]
                  >
                  > import Numeric
                  > print Numeric.dot(x,w )[/color]

                  Indeed. Horses for courses. Anyway, people who reinvent the wheel often
                  fall to arguing among themselves whose polygon is the best
                  approximation to a circle, and forget to reinvent the axle. Wouldn't
                  happen in this newsgroup, of course :-)

                  Comment

                  • Raymond Hettinger

                    #10
                    Re: computing a weighted sum

                    [Christos TZOTZIOY Georgiou][color=blue]
                    > Anyway, a functional equivalent:
                    >
                    > .>> from itertools import starmap, izip
                    > .>> import operator
                    > .>> x= [1,2,3,4]
                    > .>> w=[3.0, 6.0, 9.0, 12.0]
                    > .>> sum(starmap(ope rator.mul, izip(x,w)))
                    > 90.0[/color]

                    Gack! starmap() is only for situations where the data is already in tuple form.
                    If it inputs are already distinct, imap() is the preferred form.

                    FWIW, the answer was already in the docs (itertools recipes):

                    def dotproduct(vec1 , vec2):
                    return sum(imap(operat or.mul, vec1, vec2))


                    Raymond Hettinger




                    Comment

                    • Christos TZOTZIOY Georgiou

                      #11
                      Re: computing a weighted sum

                      On Thu, 17 Mar 2005 08:11:11 GMT, rumours say that "Raymond Hettinger"
                      <vze4rx4y@veriz on.net> might have written:
                      [color=blue]
                      >[Christos TZOTZIOY Georgiou][color=green]
                      >> Anyway, a functional equivalent:
                      >>
                      >> .>> from itertools import starmap, izip
                      >> .>> import operator
                      >> .>> x= [1,2,3,4]
                      >> .>> w=[3.0, 6.0, 9.0, 12.0]
                      >> .>> sum(starmap(ope rator.mul, izip(x,w)))
                      >> 90.0[/color]
                      >
                      >Gack! starmap() is only for situations where the data is already in tuple form.
                      >If it inputs are already distinct, imap() is the preferred form.
                      >
                      >FWIW, the answer was already in the docs (itertools recipes):
                      >
                      > def dotproduct(vec1 , vec2):
                      > return sum(imap(operat or.mul, vec1, vec2))[/color]

                      What, you're some kind of expert on itertools now? :-)

                      You are of course absolutely correct. Let my post stand as an example of
                      itertools misuse.
                      --
                      TZOTZIOY, I speak England very best.
                      "Be strict when sending and tolerant when receiving." (from RFC1958)
                      I really should keep that in mind when talking with people, actually...

                      Comment

                      Working...