脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 对python中的乘法dot和对应分量相乘multiply详解

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

2021-04-19 00:14CS青雀 Python

今天小编就为大家分享一篇对python中的乘法dot和对应分量相乘multiply详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

?
1
2
3
4
5
6
7
8
>>> 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) :

?
1
2
3
4
5
6
7
8
9
10
>>> 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) :

?
1
2
3
4
5
6
7
8
>>> 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详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ztf312/article/details/76222233

延伸 · 阅读

精彩推荐