computing a weighted sum

A

andreif

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*w

doesn't seem very pythonic :)

Thanks,
Andrei
 
W

Will McGugan

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*w

doesn't seem very pythonic :)

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
 
D

Duncan Booth

wrote:
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 : ...
x = [1, 2, 3]
w = [4, 5, 6]
sum(a*b for (a,b) in zip(x, w))
32
 
C

Christos TZOTZIOY Georgiou

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*w

doesn't seem very pythonic :)


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(operator.mul, izip(x,w)))
90.0
..>>
 
S

Steven Bethard

Will said:
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 ) )

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
 
A

andreif

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
 
F

Fernando Perez

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*w

doesn't seem very pythonic :)

Thanks,
Andrei


import Numeric
print Numeric.dot(x,w)

Best,

f
 
J

John Machin

Fernando said:
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*w

doesn't seem very pythonic :)

Thanks,
Andrei


import Numeric
print Numeric.dot(x,w)


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 :)
 
R

Raymond Hettinger

[Christos TZOTZIOY Georgiou]
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(operator.mul, izip(x,w)))
90.0

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(operator.mul, vec1, vec2))


Raymond Hettinger
 
C

Christos TZOTZIOY Georgiou

[Christos TZOTZIOY Georgiou]
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(operator.mul, izip(x,w)))
90.0

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(operator.mul, vec1, vec2))

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.
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top