我有一个矩阵T
:
[ 0.2 0.4 0.4]
[ 0.8 0.2 0. ]
[ 0.8 0. 0.2]
T = numpy.mat("0.2 0.4 0.4;0.8 0.2 0.0;0.8 0.0 0.2")
我有向量v
,numpy.array(73543, -36772, 36772)
v = numpy.array([ 73543, -36772, 36772])
如何在Python中正确地将数组v
乘以矩阵T
?
谢谢
克里斯
最佳答案
使用numpy.dot
,它与*
运算符不太一样:
In [138]: T.dot(v) #the resulting shape is (1, 3), not (3, 1) if you don't care
Out[138]: matrix([[ 14708.6, 51480. , 66188.8]])
In [139]: v.dot(T) #same with v * T
Out[139]: matrix([[ 14708.6, 22062.8, 36771.6]])
In [140]: T.dot(v[:, None]) #if you need the shape to be (3, 1) when doing T*v
Out[140]:
matrix([[ 14708.6],
[ 51480. ],
[ 66188.8]])
关于python - 如何将 numpy 数组乘以 numpy 矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923120/