在纽比.dotdocstring说:
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
但它并没有说明纽比.dot用二维计算一维阵列阵列。所以Numpy如何处理一维数组(向量)和二维数组(矩阵)?在
我做了一些测试:
In [27]: a
Out[27]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [28]: b
Out[28]: array([0, 1, 2])
In [29]: np.dot(a,b)
Out[29]: array([ 5, 14, 23])
In [30]: np.dot(a, b.reshape(-1,1))
Out[30]:
array([[ 5],
[14],
[23]])
In [31]: np.dot(a, b.reshape(-1,1)).ravel() # same as np.dot(a,b)
Out[31]: array([ 5, 14, 23])
In [32]: np.dot(b,a)
Out[32]: array([15, 18, 21])
In [33]: np.dot(b.reshape(1,-1), a)
Out[33]: array([[15, 18, 21]])
In [34]: np.dot(b.reshape(1,-1), a).ravel() # same as np.dot(b,a)
Out[34]: array([15, 18, 21])
以上测试表明纽比.dot可以用二维数组处理一维数组。对吗?在
一维数组和二维数组作为矩阵向量(或向量矩阵)的乘积来处理。实际上,该实现使用BLAS
*gemv
函数来处理浮点输入的这种情况。在只有一种情况在文档中没有明确描述,但有点暗示,那就是如何将规则应用于2D和1D输入:
在您的例子中,当您执行}的和积进行运算,正如您的测试很容易显示的那样。在
np.dot(a, b)
时,b
没有“倒数第二”轴。纽比做的,就是最后一次。因此,它对a
的每一行与{所有其他例子都符合上述规则。在
相关问题 更多 >
编程相关推荐