网络编程
位置:首页>> 网络编程>> Python编程>> 对python中的乘法dot和对应分量相乘multiply详解

对python中的乘法dot和对应分量相乘multiply详解

作者:CS青雀  发布时间:2021-01-14 15:54:58 

标签:python,dot,multiply

向量点乘 (dot) 和对应分量相乘 (multiply) :


>>> a
array([1, 2, 3])
>>> b
array([ 1., 1., 1.])
>>> np.multiply(a,b)
array([ 1., 2., 3.])
>>> np.dot(a,b)
6.0

矩阵乘法 (dot) 和对应分量相乘 (multiply) :


>>> c
matrix([[1, 2, 3]])
>>> d
matrix([[ 1., 1., 1.]])
>>> np.multiply(c,d)
matrix([[ 1., 2., 3.]])
>>> np.dot(c,d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

写代码过程中,*表示对应分量相乘 (multiply) :


>>> a*b
array([ 1., 2., 3.])
>>> c*d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py", line 343, in __mul__
 return N.dot(self, asmatrix(other))
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

来源:https://blog.csdn.net/ztf312/article/details/76222233

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com