python numpy中multiply与*及matul 的区别说明


Posted in Python onMay 26, 2021

1、对于矩阵(matrix)而言

multiply是对应元素相乘,而 * 、np.matmul() 函数 与 np.dot()函数 相当于矩阵乘法(矢量积),对应的列数和行数必须满足乘法规则;如果希望以数量积的方式进行,则必须使用 np.multiply 函数,如下所示:

a = np.mat([[1, 2, 3, 4, 5]])
b = np.mat([[1,2,3,4,5]])
c=np.multiply(a,b)
print(c)

结果是

[[ 1 4 9 16 25]]
a = np.mat([[1, 2, 3, 4, 5]])
b = np.mat([ [1],[2],[3],[4],[5] ] )
d=a*b
print(d) #a是shape(1,5),b是shape(5,1),结果是一个实数

结果是

[[55]]

2、对于数组(Array)而言

* 与 multiply均表示的是数量积(即对应元素的乘积相加),np.matmul与np.dot表示的是矢量积(即矩阵乘法)。

代码:

if __name__ == '__main__':
    w = np.array([[1,2],[3,4]])
    x = np.array([[1,3],[2,4]])
    w1 = np.array([[1,2],[3,4]])
    x1 = np.array([[1,2]])
    w_mat = np.mat([[1,2],[3,4]])
    x_mat = np.mat([[1,3],[2,4]])
    print("x1.shape:",np.shape(x1))
    w_x_start = w*x
    w_x_dot = np.dot(w,x)
    x_w_dot = np.dot(x,w)
    w_x_matmul = np.matmul(w, x)
    x_w_matmul = np.matmul(x, w)
    w_x_multiply = np.multiply(w,x)
    x_w_multiply = np.multiply(x, w)
    #w1_x1_matmul = np.matmul(w1, x1)
    x1_w1_matmul = np.matmul(x1, w1)
    w_x_mat_matmul = np.matmul(w_mat,x_mat)
    x_w_mat_matmul = np.matmul(x_mat, w_mat)
    w_x_mat_start = w_mat*x_mat
    x_w_mat_start = x_mat*w_mat
    w_x_mat_dot = np.dot(w_mat,x_mat)
    x_w_mat_dot = np.dot(x_mat,w_mat)
    w_x_mat_multiply = np.multiply(w_mat,x_mat)
    x_w_mat_multiply = np.multiply(x_mat,w_mat)
 
    print("W.shape:", np.shape(w))
    print("x.shape:", np.shape(x))
    print("w_x_start.shape:", np.shape(w_x_start))
    print("w_x_dot.shape:", np.shape(w_x_dot))
    print("x_w_dot.shape:", np.shape(x_w_dot))
    print("x1_w1_matmul.shape::", np.shape(x1_w1_matmul))
 
    print("做Array数组运算时:", '\n')
    print("w_x_start:", w_x_start)
    print("w_x_dot:", w_x_dot)
    print("x_w_dot:", x_w_dot)
    print("w_x_matmul:", w_x_matmul)
    print("x_w_matmul:", x_w_matmul)
    print("w_x_multiply:", w_x_multiply)
    print("x_w_multiply:", x_w_multiply)
    # print("w1_x1_matmul:", w1_x1_matmul)
    print("x1_w1_matmul:", x1_w1_matmul)
 
    print("做matrix矩阵运算时:", '\n')
    print("w_x_mat_start:", w_x_mat_start)
    print("x_w_mat_start:", x_w_mat_start)
    print("x_w_mat_dot:", x_w_mat_dot)
    print("w_x_mat_dot:", w_x_mat_dot)
    print("w_x_mat_matmul:",w_x_mat_matmul)
    print("x_w_mat_matmul:", x_w_mat_matmul)
    print("w_x_mat_multiply",w_x_mat_multiply)
    print("x_w_mat_multiply", x_w_mat_multiply)
x1.shape: (1, 2)
W.shape: (2, 2)
x.shape: (2, 2)
w_x_start.shape: (2, 2)
w_x_dot.shape: (2, 2)
x_w_dot.shape: (2, 2)
x1_w1_matmul.shape:: (1, 2)
做Array数组运算时:
 
w_x_start: [[ 1  6]
 [ 6 16]]
w_x_dot: [[ 5 11]
 [11 25]]
x_w_dot: [[10 14]
 [14 20]]
w_x_matmul: [[ 5 11]
 [11 25]]
x_w_matmul: [[10 14]
 [14 20]]
w_x_multiply: [[ 1  6]
 [ 6 16]]
x_w_multiply: [[ 1  6]
 [ 6 16]]
x1_w1_matmul: [[ 7 10]]
做matrix矩阵运算时:
 
w_x_mat_start: [[ 5 11]
 [11 25]]
x_w_mat_start: [[10 14]
 [14 20]]
x_w_mat_dot: [[10 14]
 [14 20]]
w_x_mat_dot: [[ 5 11]
 [11 25]]
w_x_mat_matmul: [[ 5 11]
 [11 25]]
x_w_mat_matmul: [[10 14]
 [14 20]]
w_x_mat_multiply [[ 1  6]
 [ 6 16]]
x_w_mat_multiply [[ 1  6]
 [ 6 16]]

python中转置的优先级高于乘法运算 例如:

a = np.mat([[2, 3, 4]])
b = np.mat([[1,2,3]] )
d=a*b.T
print(d)

结果是

[[20]]

其中a为1行3列,b也为1行3列,按理来说直接计算a*b是不能运算,但是计算d=a*b.T是可以的,结果是20,说明运算顺序是先转置再计算a与b转置的积,*作为矩阵乘法,值得注意的在执行*运算的时候必须符合行列原则。

numpy中tile()函数的用法

b = tile(a,(m,n)):即是把a数组里面的元素复制n次放进一个数组c中,然后再把数组c复制m次放进一个数组b中,通俗地讲就是将a在行方向上复制m次,在列方向上复制n次。

python中的 sum 和 np.sum 是不一样的,如果只写sum的话,表示的是数组中对应的维度相加,如果写 np.sum 的话,表示一个数组中的维数和列数上的数都加在一起。

如下图所示:

python numpy中multiply与*及matul 的区别说明

补充:总结:numpy中三个乘法运算multiply,dot和* 的区别

引言:

本人在做机器学习的练习1的时候,时常抛出错误:

python numpy中multiply与*及matul 的区别说明

Not aligned是什么意思呢?

意思是两个矩阵相乘无意义。

线性代数中mxn 和 nxp的矩阵才能相乘,其结果是mxp的矩阵。

出错源代码:

def gradientDescent(X,y,theta,alpha,iteration):
    colunms = int(theta.ravel().shape[1])
    thetai = np.matrix(np.zeros(theta.shape))
    cost = np.zeros(iteration)
                       
    for i in range(iteration):
        error = X*theta.T-y
        for j in range(colunms):
            a = np.sum(error*X[:,j])/len(X) ########## error!
            thetai[0,j] = thetai[0,j] - alpha*a
            
        theta = thetai    
        cost[i] = computeCost(X, y, theta)        
    return theta,cost

这里error是一个nx1的矩阵,theta.T也是一个nx1的矩阵。

而矩阵之间*运算符表示矩阵乘法。我们这里想实现矩阵的对应元素相乘,因此应该用np.multiply()实现。

总结:

(读者可使用简单的举例自行验证)

1.*用法:

矩阵与矩阵:矩阵乘法(matrix)

数组与数组:对应位置相乘(array)

2.np.dot()用法:

矩阵相乘的结果

3.np.multiply()用法:

数组、矩阵都得到对应位置相乘。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python Mysql数据库操作 Perl操作Mysql数据库
Jan 12 Python
python使用webbrowser浏览指定url的方法
Apr 04 Python
Python实现遍历数据库并获取key的值
May 17 Python
python使用matplotlib绘制柱状图教程
Feb 08 Python
Python单例模式的两种实现方法
Aug 14 Python
Python+PIL实现支付宝AR红包
Feb 09 Python
Tensorflow使用tfrecord输入数据格式
Jun 19 Python
解决Pycharm运行时找不到文件的问题
Oct 29 Python
Python 创建新文件时避免覆盖已有的同名文件的解决方法
Nov 16 Python
python使用Plotly绘图工具绘制散点图、线形图
Apr 02 Python
Python 获取numpy.array索引值的实例
Dec 06 Python
利用python进行数据加载
Jun 20 Python
python文本处理的方案(结巴分词并去除符号)
Django操作cookie的实现
May 26 #Python
pandas中DataFrame检测重复值的实现
python 中的@运算符使用
May 26 #Python
Python 实现定积分与二重定积分的操作
May 26 #Python
python 解决微分方程的操作(数值解法)
python 实现体质指数BMI计算
May 26 #Python
You might like
php curl选项列表(超详细)
2013/07/01 PHP
ThinkPHP使用getlist方法实现数据搜索功能示例
2017/05/08 PHP
让你的网站可编辑的实现js代码
2009/10/19 Javascript
一些javascript一些题目的解析
2010/12/25 Javascript
jquery 之 $().hover(func1, funct2)使用方法
2012/06/14 Javascript
jQuery实现点击文本框弹出热门标签的提示效果
2013/11/17 Javascript
jQuery中:reset选择器用法实例
2015/01/04 Javascript
详解JavaScript的AngularJS框架中的作用域与数据绑定
2016/03/04 Javascript
详解Jquery实现ready和bind事件
2016/04/14 Javascript
IScroll5 中文API参数说明和调用方法
2016/05/21 Javascript
深入浅出es6模板字符串
2017/08/26 Javascript
基于jQuery实现图片推拉门动画效果的两种方法
2017/08/26 jQuery
浅谈JavaScript的innerWidth与innerHeight
2017/10/12 Javascript
vue实现axios图片上传功能
2019/08/20 Javascript
Angular5整合富文本编辑器TinyMCE的方法(汉化+上传)
2020/05/26 Javascript
python 实现插入排序算法
2012/06/05 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
2020/04/23 Python
python快排算法详解
2019/03/04 Python
python 读取修改pcap包的例子
2019/07/23 Python
利用python计算时间差(返回天数)
2019/09/07 Python
Python根据服务获取端口号的方法
2019/09/25 Python
Python使用selenium + headless chrome获取网页内容的方法示例
2019/10/16 Python
Python编译成.so文件进行加密后调用的实现
2019/12/23 Python
win10下python3.8的PIL库安装过程
2020/06/08 Python
python+selenium自动化实战携带cookies模拟登陆微博
2021/01/19 Python
检测用户浏览器是否支持CSS3的方法
2009/08/29 HTML / CSS
HTML5是否真的可以取代Flash
2010/02/10 HTML / CSS
意大利体育用品和运动服网上商店:Maxi Sport
2019/09/14 全球购物
世界排名第一的运动鞋市场:Flight Club
2020/01/03 全球购物
给民警的表扬信
2014/01/08 职场文书
授权委托书样本
2014/09/25 职场文书
总经理致辞
2015/07/29 职场文书
2015年教师个人业务工作总结
2015/10/23 职场文书
党员电教片《信仰》心得体会
2016/01/15 职场文书
零基础学java之方法的定义与调用详解
2022/04/10 Java/Android
利用Java连接Hadoop进行编程
2022/06/28 Java/Android