当前位置: 移动技术网 > IT编程>脚本编程>Python > 对python中的乘法dot和对应分量相乘multiply详解

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

2018年11月30日  | 移动技术网IT编程  | 我要评论

五常贡米,手机支撑架,张家界天气预报10天

向量点乘 (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)

以上这篇对python中的乘法dot和对应分量相乘multiply详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网