Python 对于Numpy,什么’;计算大小为10的向量与大小为(5,10)的矩阵中的每一行的内积的最佳方法是什么?
我们只使用内积运算符Python 对于Numpy,什么’;计算大小为10的向量与大小为(5,10)的矩阵中的每一行的内积的最佳方法是什么?,python,numpy,Python,Numpy,我们只使用内积运算符@。例如, import numpy as np v = np.random.random(size = 10) # vector of size 10 m = np.random.random(size = (5, 10)) # matrix of size (5, 10) m@v # compute inner product 在这种情况下,m[i]@v=(m@v)[i] 对于i=0,1,2,3,4 >>> v array([ 0.6210628
@
。例如,
import numpy as np
v = np.random.random(size = 10) # vector of size 10
m = np.random.random(size = (5, 10)) # matrix of size (5, 10)
m@v # compute inner product
在这种情况下,m[i]@v=(m@v)[i]
对于i=0,1,2,3,4
>>> v
array([ 0.62106289, 0.53194633, 0.26856774, 0.97947622, 0.38479922,
0.03201307, 0.51045447, 0.49990235, 0.54168934, 0.50868176])
>>> m
array([[ 0.13075822, 0.68711111, 0.15934606, 0.44172015, 0.22615204,
0.44816934, 0.53960113, 0.80397538, 0.50143107, 0.18997813],
[ 0.78891921, 0.035147 , 0.40099655, 0.33270442, 0.42478057,
0.4145495 , 0.84061422, 0.36837421, 0.62218952, 0.34043758],
[ 0.96878536, 0.74850168, 0.52667831, 0.87088367, 0.28361016,
0.4861426 , 0.30693948, 0.81342318, 0.66667058, 0.32418454],
[ 0.44617067, 0.87359669, 0.91079363, 0.70184155, 0.65774634,
0.83738866, 0.29639453, 0.20157455, 0.08307142, 0.46592851],
[ 0.85389639, 0.59084659, 0.76528861, 0.70007616, 0.23339048,
0.49946522, 0.35635152, 0.38155495, 0.46604566, 0.00703169]])
>>> m@v
array([ 2.06914458, 2.24241604, 3.20833979, 2.48783289, 2.47033001])
另一种解决方案使用np.einsum
。更容易理解的是,你在过去使用过爱因斯坦的求和符号(比如微分几何或更高级的物理学)
首先,我们可以问哪一个有效
>>> np.einsum('ij,j',m,v)
array([ 2.06914458, 2.24241604, 3.20833979, 2.48783289, 2.47033001])
首先,使用点的错误方法是:
In [4]: vector1 = np.arange(10)
In [5]: matrix1 = np.arange(50).reshape(5,10)
另一个错误的方法是,matmul
的基本规则与dot
的基本规则是“a的最后一个,b的第二个到最后一个”:
In [7]: np.array([np.dot(row,vector1) for row in matrix1])
Out[7]: array([ 285, 735, 1185, 1635, 2085])
还有另一种正确的方法
In [9]: np.dot(matrix1,vector1)
Out[9]: array([ 285, 735, 1185, 1635, 2085])
那些起作用的产生同样的结果
[9]中的顺序也适用于matmul
。它也可以写成matrix1@vector1
至于“最佳”,我们可以试试时机。我猜[9]是最快的,[7]是最慢的
(5,10)与(10,)配对的情况是(5,10)与(10,n)配对产生(5,n)的典型2d情况的推广。1)会产生错误。你的问题与我的问题非常相似,我不得不比较用户名。更好的意思是什么?更清晰?更快?更精确的数字?更容易打字?无论答案是什么,您都可以轻松地自己对其进行基准测试。这是Udacity的PyTorch应用程序简介中的一个问题。复制粘贴完全正确。下一次,请先尝试自己运行代码。无论如何,欢迎来到StackOverFrlow Rohit。
In [6]: np.dot(vector1*matrix1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-c0ee48ce8d26> in <module>()
----> 1 np.dot(vector1*matrix1)
TypeError: Required argument 'b' (pos 2) not found
In [7]: np.array([np.dot(row,vector1) for row in matrix1])
Out[7]: array([ 285, 735, 1185, 1635, 2085])
In [8]: np.matmul(vector1,matrix1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-90d753017fdf> in <module>()
----> 1 np.matmul(vector1,matrix1)
ValueError: shapes (10,) and (5,10) not aligned: 10 (dim 0) != 5 (dim 0)
In [9]: np.dot(matrix1,vector1)
Out[9]: array([ 285, 735, 1185, 1635, 2085])
In [10]: np.sum(matrix1*vector1,axis=1)
Out[10]: array([ 285, 735, 1185, 1635, 2085])