numpy中dot, multiply, *区别
1.dot
首先看下dot源码中的注释部分
def dot(a, b, out=None):"""dot(a, b, out=None)Dot product of two arrays. Specifically,- If both `a` and `b` are 1-D arrays, it is inner product of vectors(without complex conjugation).- If both `a` and `b` are 2-D arrays, it is matrix multiplication,but using :func:`matmul` or ``a @ b`` is preferred.- If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply`and using ``numpy.multiply(a, b)`` or ``a * b`` is preferred.- If `a` is an N-D array and `b` is a 1-D array, it is a sum product overthe last axis of `a` and `b`.- If `a` is an N-D array and `b` is an M-D array (where ``M>=2``), it is asum product over the last axis of `a` and the second-to-last axis of `b`::dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]).....
关注一下最常用的两种情况:
If both
aand
bare 1-D arrays, it is inner product of vectors
这就是两个向量dot,最后得到的两个向量的内积。
If both
aand
bare 2-D arrays, it is matrix multiplication, but using :func:
matmulor ``a @ b`` is preferred.
2-D arrays指的就是矩阵了。根据上面的解释不难看出,如果是两个矩阵dot,执行的就是矩阵相乘运算。
写段代码测试下
def demo2():a1 = np.arange(1, 5)a2 = a1[::-1]print(a1)print(a2)# 两个向量dot为内积print(a1.dot(a2))print(np.dot(a1, a2))print("\n\n")b1 = np.arange(1, 5).reshape(2, 2)b2 = np.arange(5, 9).reshape(2, 2)b3 = np.arange(9, 15).reshape(3, 2)print(b1)print(b2)print(b3)print(np.dot(b1, b2))# 会报错, 不满足矩阵相乘条件# print(np.dot(b1, b3))
代码执行的结果
[1 2 3 4]
[4 3 2 1]
20
20[[1 2][3 4]]
[[5 6][7 8]]
[[ 9 10][11 12][13 14]]
[[19 22][43 50]]
2.multiply
同样的看一下multiply对应源码的注释部分。
def multiply(x1, x2, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])Multiply arguments element-wise.Parameters----------x1, x2 : array_likeInput arrays to be multiplied. If ``x1.shape != x2.shape``, they must be broadcastable to a common shape (which becomes the shape of the output).out : ndarray, None, or tuple of ndarray and None, optional.....
明白multiply方法的关键就是上面的一句注释:
Multiply arguments element-wise.
说人话就是:按对应的元素相乘。
def demo3():a1 = np.arange(1, 5)a2 = a1[::-1]print(a1)print(a2)print(np.multiply(a1, a2))print("\n\n")b1 = np.arange(1, 5).reshape(2, 2)b2 = np.arange(5, 9).reshape(2, 2)print(b1)print(b2)print(np.multiply(b1, b2))
运行得到结果
[1 2 3 4]
[4 3 2 1]
[4 6 6 4][[1 2][3 4]]
[[5 6][7 8]]
[[ 5 12][21 32]]
参考对应的代码,应该就很容易理解了。
3. *运算符
乘法运算符,最后得到的结果,跟multiply方法得到的结果是一样的。
def demo4():a1 = np.arange(1, 5)a2 = a1[::-1]print(a1)print(a2)print(a1 * a2)print("\n\n")b1 = np.arange(1, 5).reshape(2, 2)b2 = np.arange(5, 9).reshape(2, 2)print(b1)print(b2)print(b1 * b2)
最终结果
[1 2 3 4]
[4 3 2 1]
[4 6 6 4][[1 2][3 4]]
[[5 6][7 8]]
[[ 5 12][21 32]]
numpy中dot, multiply, *区别相关推荐
- python中multiply函数_python中numpy中的multiply、*、matul 的区别
numpy中的multiply.*.matul 的区别 1.对于矩阵(matrix)而言,multiply是对应元素相乘,而 * .np.matmul() 函数 与 np.dot()函数 相当于矩阵 ...
- 对NumPy中dot()函数的理解(亲测,矩阵算法)
今天学习到numpy基本的运算方法,遇到了一个让我比较难理解的问题.就是dot函数是如何对矩阵进行运算的. 一.dot()的使用 参考文档:https://docs.scipy.org/doc/num ...
- Numpy中矩阵向量乘法np.dot()及np.multiply()以及*区别
Numpy中的矩阵向量乘法分别是np.dot(a,b).np.multiply(a,b) 以及*,刚开始接触的时候比较模糊,于是自己整理了一下.先来介绍理论,然后再结合例子深入了解一下. 数组 矩阵 ...
- numpy中matmul,dot,multiply的区别
结论: np.matmul用于矩阵相乘,dot即适用于矩阵相乘也使用标量与矩阵相乘.*,multiply用于两个矩阵中对应的元素相乘. 上面这句话的理解:np.matmul(a,b) a.sha ...
- python Numpy 中的矩阵向量乘法(np.multiply()、np.dot()、np.matmul() 和 星号(*)、@)
python Numpy 中的矩阵向量乘法 总结 1. 对于 np.array 对象 1.1 元素乘法 用 a*b 或 np.multiply(a,b) 1.2 矩阵乘法 用 np.dot(a,b) ...
- numpy矩阵乘法中的multiply,matmul和dot
用numpy做矩阵运算时,少不了用到矩阵乘法.本文帮你迅速区分multiply, matmul和dot的区别. numpy官方文档中的说明:(想深入了解可以一戳) multiply: https:// ...
- numpy中*,np.multiply和np.dot辨析
目录 1 总结 2 向量 2.1 向量的内积(点乘) 2.2 向量的外积(叉乘):np.cross(a,b) 2.3 向量元素对应位置相乘 2.4 示例 3 矩阵 3.1 矩阵乘法(矩阵叉乘) 3.2 ...
- Numpy数据的操作 * dot() multiply() 的区别
使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,* ...
- python中matrix是什么意思_Python与线性代数——Numpy中的matrix()和array()的区别
Numpy中matrix必须是2维的,但是 numpy中array可以是多维的(1D,2D,3D····ND).matrix是array的一个小的分支,包含于array.所以matrix 拥有arra ...
- 一文搞定python中的multiply()和dot以及用法
python中的multiply()和dot以及*用法.md 首先创建如下的数组和矩阵,其中a,b为数组,A,B为矩阵 import numpy as npa = np.arange(1,5).res ...
最新文章
- 【2019雅礼集训】【CF 960G】【第一类斯特林数】【NTT多项式】permutation
- 网站的高性能架构---存储性能优化
- Python 技术篇-解决转化后的exe程序被360误杀问题实例演示,python生成的exe被360识别为勒索病毒原因及解决方法
- 学习响应式BootStrap来写融职教育网站,Bootsrtap第十天你的收获
- 【渝粤题库】广东开放大学 岭南文化概论专科 形成性考核
- PAZU 是4Fang 为配合“四方在线”软件于2004年开发的WEB打印控件,适用于各种WEB软件项目的打印。...
- linux 安装rmp服务,Linux LAMP服务的rpm包安装与配置
- 8.2 css3 新增标签 盒子模型 长度单位 颜色 渐变 径像渐变
- 一个简单JavaAgent的实现
- php获取flash上传视频文件大小,如何获取flash文件(.swf文件)的长度和宽度
- 视频移动侦测VMD的实现
- if else语句linux,linux 中的if else语句
- linux 进程的pid分配策略——pid位图算法
- 同学,你要的SpringBoot多图片上传回显功能已经实现了,赶紧收藏吃灰~
- python commands执行不连续_python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。...
- 运行JProfiler:ERROR: Invalid license key. Aborting
- 【图像处理】基于matlab分形插值算法调换图片【含Matlab源码 197期】
- 微信小程序安装Vant组件库
- tp5简单的图片上传
- 探索ELF可执行文件的“干货”:段头表和段的基本介绍