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

(编辑:jimmy 日期: 2024/9/29 浏览:2)

向量点乘 (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详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。