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

yipeiwu_com6年前Python基础

向量点乘 (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详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

TensorFlow安装及jupyter notebook配置方法

tensorflow利用anaconda在ubuntu下安装方法及jupyter notebook运行目录及远程访问配置 Ubuntu下安装Anaconda bash ~/file_...

python实现在pandas.DataFrame添加一行

实例如下所示: from pandas import * from random import * df = DataFrame(columns=('lib', 'qty1', 'q...

python列出目录下指定文件与子目录的方法

本文实例讲述了python列出目录下指定文件与子目录的方法。分享给大家供大家参考。具体实现方法如下: # if you know the exact name: import os...

Python探索之URL Dispatcher实例详解

URL dispatcher简单点理解就是根据URL,将请求分发到相应的方法中去处理,它是对URL和View的一个映射,它的实现其实也很简单,就是一个正则匹配的过程,事先定义好正则表达式...

python3实现带多张图片、附件的邮件发送

本文实例为大家分享了python3实现多张图片附件邮件发送的具体代码,供大家参考,具体内容如下 直接上代码,没有注释! from email.mime.text import MIM...