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

numpy.dot:

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.

(强调我。)

例如,使用2D数组考虑这个例子:

>>> 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更快,但我的机器是相当负载在这一刻,所以时间是不一致,也不是必然非常准确。

你可能感兴趣的:(python中numpy,dot)