numpy dot()和inner()之间的区别 [英] difference between numpy dot() and inner()

查看:183
本文介绍了numpy dot()和inner()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

import numpy as np
np.dot(a,b)

import numpy as np
np.inner(a,b)

我尝试过的所有示例都返回了相同的结果. Wikipedia 有相同的文章吗?在 inner()的描述中说,它的行为在较高维度上有所不同,但我无法产生任何不同的输出.我应该使用哪一个?

all examples I tried returned the same result. Wikipedia has the same article for both?! In the description of inner() it says, that its behavior is different in higher dimensions, but I couldn't produce any different output. Which one should I use?

推荐答案

numpy.dot :

对于2-D数组,它等效于矩阵乘法,对于1-D数组,其等效于向量的内积(无复共轭).对于N个维度,它是a的最后一个轴和b的倒数第二个的总和:

numpy.inner :

一维数组的向量的普通内积(无复共轭),在更高维度上,最后一个轴上的和积.

(强调我的.)

作为示例,请考虑具有2D数组的示例:

As an example, consider this example with 2D arrays:

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

因此,您应该使用的一种为您的应用程序提供正确行为的软件.

Thus, the one you should use is the one that gives the correct behaviour for your application.

性能测试

(请注意,我只测试一维情况,因为这是.dot.inner给出相同结果的唯一情况.)

(Note that I am testing only the 1D case, since that is the only situation where .dot and .inner give the same result.)

>>> 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更快,但是我的机器目前负载相当大,所以时间安排不一致,也不一定很准确.

So maybe .inner is faster, but my machine is fairly loaded at the moment, so the timings are not consistent nor are they necessarily very accurate.

这篇关于numpy dot()和inner()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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