python中numpy dot_numpy dot()和inner()之间的区别

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:Ordinary inner product of vectors for 1-D arrays (without complex conjugation), in higher dimensions a sum product over the last axes.

(强调我的。)

作为一个例子,考虑这个二维数组的例子:>>> a=np.array([[1,2],[3,4]])

>>> b=np.array([[11,12],[13,14]])

>>> np.dot(a,b)

array([[37, 40],

[85, 92]])

>>> np.inner(a,b)

array([[35, 41],

[81, 95]])

因此,您应该使用的是为您的应用程序提供正确行为的那个。

性能测试

(注意,我只测试1D的情况,因为这是.dot和.inner给出相同结果的唯一情况。)>>> import timeit

>>> setup = 'import numpy as np; a=np.random.random(1000); b = np.random.random(1000)'

>>> [timeit.timeit('np.dot(a,b)',setup,number=1000000) for _ in range(3)]

[2.6920320987701416, 2.676928997039795, 2.633111000061035]

>>> [timeit.timeit('np.inner(a,b)',setup,number=1000000) for _ in range(3)]

[2.588860034942627, 2.5845699310302734, 2.6556360721588135]

所以也许.inner更快,但我的机器目前负载相当大,所以计时不一致,也不一定非常准确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值