点产品 [英] dot products

查看:42
本文介绍了点产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI。

我想计算存储为列表a和ba

和b的两个向量的点积。


一个简单的方法是

sum(a [i] * b [i] for i in range(len(a)))


另一个简单的方法是

ans = 0.0

我在范围内(len(a)):

ans = ans + a [ i] * b [i]


但是还有其他方法比上述方法更快。 (通过

分析它们的方式我发现第二个更快了大约30%。)

rahul

HI.
I want to compute dot product of two vectors stored as lists a and b.a
and b are of the same length.

one simple way is
sum(a[i]*b[i] for i in range(len(a)))

another simple way is
ans=0.0
for i in range(len(a)):
ans=ans+a[i]*b[i]

But is there any other way which is faster than any of the above. (By
the way profiling them i found that the second is faster by about 30%.)
rahul

推荐答案

Rahul写道:
Rahul wrote:
我想计算存储为列表a和ba
的两个向量的点积和b是相同的长度。

一个简单的方法是总和(a [i] * b [i] for i in range(len(a)))
I want to compute dot product of two vectors stored as lists a and b.a
and b are of the same length.

one simple way is
sum(a[i]*b[i] for i in range(len(a)))




顺便说一句,imho最Pythonic将是:


sum(i * j为(i,j)in zip(a,b))



btw, imho the most "Pythonic" would be:

sum(i*j for (i,j) in zip(a,b))


Rahul写道:
HI。
我想计算存储为列表a和ba
的两个向量的点积和b具有相同的长度。
一个简单的方法是总和(a [i] * b [i] for i in range(len(a)))

另一个简单的方法是
ans = 0.0
我在范围内(len(a)):
ans = ans + a [i] * b [i]

但是还有其他比任何上述内容。


尝试:


总和(x * y表示x,y表示拉链(a,b))

在zip()(锁定迭代几个序列)和枚举()

(迭代序列,但也提供索引计数器)之间,很少见

你想在生成器表达式中使用索引表示法。

(顺便说一下,我发现第二个更快了大约30%。)
HI.
I want to compute dot product of two vectors stored as lists a and b.a
and b are of the same length.

one simple way is
sum(a[i]*b[i] for i in range(len(a)))

another simple way is
ans=0.0
for i in range(len(a)):
ans=ans+a[i]*b[i]

But is there any other way which is faster than any of the above.
Try:

sum(x * y for x, y in zip(a, b))

Between zip() (lockstep iteration over several sequences) and enumerate()
(iteration over a sequence, but also providing an index counter), it is rare
that you will want to use indexing notation in a generator expression.
(By the way profiling them i found that the second is faster by about 30%.)



对于短序列,生成器表达式可能比列表

comprehensions或for循环稍慢,因为后两者不涉及
$的开销。 b $ b设置生成器并从中检索值。随着序列长度的增加,生成器表达式最终会因为内存减少而减少内存影响。


干杯,

尼克。


-

Nick Coghlan | nc******@email.com |澳大利亚布里斯班

--------------------------------------- ------------------------
http://boredomandlaziness.skystorm.net



For short sequences, generator expressions may end up slightly slower than list
comprehensions or for loops, as the latter two do not involve the overhead of
setting up the generator and retrieving values from it. As the sequences
increase in length, generator expressions generally win in the end due to their
reduced memory impact.

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net


Rahul写道:
Rahul wrote:
我想要计算存储为列表a和ba
和b的两个向量的点积。长度相同。

一个简单的方法是总和(a [i] * b [i对于范围内的我(len(a)))

另一个简单的方法是对范围内的我(len(a)):
ans = ans + a [i] * b [i]

但是还有其他方法比上述方法更快。 (通过描述它们的方式我发现第二个更快了大约30%。)
I want to compute dot product of two vectors stored as lists a and b.a
and b are of the same length.

one simple way is
sum(a[i]*b[i] for i in range(len(a)))

another simple way is
ans=0.0
for i in range(len(a)):
ans=ans+a[i]*b[i]

But is there any other way which is faster than any of the above. (By
the way profiling them i found that the second is faster by about 30%.)




你可以试试


sigma = 0

for ai,bi in itertools.izip(a,b):

sigma + = ai * bi




总和(itertools.starmap(operator.mul,itertools.izip(a,b)))


但如果你真的担心数字运算效率,请使用

Numarray。


彼得



You could try

sigma = 0
for ai, bi in itertools.izip(a, b):
sigma += ai * bi

or

sum(itertools.starmap(operator.mul, itertools.izip(a, b)))

but if you are really concerned about number-crunching efficiency, use
Numarray.

Peter


这篇关于点产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆