np.dot()函数的用法详解
作者:100yes001 发布时间:2023-06-14 02:12:21
基本简介
dot函数为numpy库下的一个函数,主要用于矩阵的乘法运算,其中包括:向量内积、多维矩阵乘法和矩阵与向量的乘法。
1. 向量内积
向量其实是一维的矩阵,两个向量进行内积运算时,需要保证两个向量包含的元素个数是相同的。
例1:
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([2, 3, 4, 5, 6, 7, 8])
result = np.dot(x, y)
print(result)
输出结果:
168
计算过程就是将向量中对应元素相乘,再相加所得。即普通的向量乘法运算。
2. 矩阵乘法运算
两个矩阵(x, y)如果可以进行乘法运算,需要满足以下条件:
x为 m×n 阶矩阵,y为 n×p 阶矩阵,
则相乘的结果 result 为 m×p 阶矩阵。
例2:
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4]])
y = np.array([[0, 1, 1, 1],
[1, 2, 0, 1],
[0, 0, 2, 1]])
result = np.dot(x, y)
print(result)
print("x阶数:" + str(x.shape))
print("y阶数:" + str(y.shape))
print("result阶数:" + str(result.shape))
结果为:
[[ 2 5 7 6]
[ 4 11 11 11]]
x阶数:(2, 3)
y阶数:(3, 4)
result阶数:(2, 4)
dot(x, y)不等于dot(y, x),矩阵乘法不满 * 换律
例3:
import numpy as np
x = np.array([[1, 2],
[3, 4]])
y = np.array([[2, 2],
[1, 2]])
result1 = np.dot(x, y)
result2 = np.dot(y, x)
print("result1 = " + str(result1))
print("result2 = " + str(result2))
结果为:
result1 = [[ 4 6]
[10 14]]
result2 = [[ 8 12]
[ 7 10]]
如果不满足运算前提,都不可以运算。例2的dot(y,x)不满足运算条件,因此运算会报错。
例4:
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4]])
y = np.array([[0, 1, 1, 1],
[1, 2, 0, 1],
[0, 0, 2, 1]])
result = np.dot(y, x)
print(result)
结果为:
Traceback (most recent call last):
File "numpy1.py", line 96, in <module>
result = np.dot(y,x)
File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (3,4) and (2,3) not aligned: 4 (dim 1) != 2 (dim 0)
3. 矩阵与向量乘法
矩阵x为m×n阶,向量y为n阶向量,则矩阵x和向量y可以进行乘法运算,结果为m阶向量。进行运算时,会首先将后面一项进行自动转置操作,之后再进行乘法运算。
例5:
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4]])
y = np.array([1, 2, 3])
result = np.dot(x, y)
print(result)
print("x阶数:" + str(x.shape))
print("y阶数:" + str(y.shape))
print("result阶数:" + str(result.shape))
结果为:
[14 23]
x阶数:(2, 3)
y阶数:(3,)
result阶数:(2,)
例6:仍然不满 * 换律
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4],
[0, 1, 1]])
y = np.array([1, 2, 3])
result1 = np.dot(x, y) # 1×1 + 2×2 + 3×3 = 14(result1的第一个元素)
result2 = np.dot(y, x) # 1×1 + 2×3 + 3×0 = 7 (result2的第一个元素)
print("result1 = " + str(result1))
print("result2 = " + str(result2))
结果为:
result1 = [14 23 5]
result2 = [ 7 13 14]
来源:https://blog.csdn.net/weixin_42755982/article/details/104004042


猜你喜欢
- 用于制作自动化微信聊天图片,通过图片生成段子视频根据一个txt文档input.txtL 一路走过来好热啊
- 文件操作此为本人学习python过程中的笔记,将持续更新,欢迎提问指正。1.txt文件1.文本文件 txt2.二进制文件 图片视频操作流程打
- PDOStatement::debugDumpParamsPDOStatement::debugDumpParams — 打印一条 SQL
- udf_WeekDayName 代码如下:CREATE FUNCTION [dbo].[udf_WeekDayName] ( ) RETUR
- 在 Python 中,我们可以使用基本的索引操作来获取数组中的元素。然而,有时候我们需要获取一个数组的子数组,也就是只获取数组中的一部分元素
- 一、python多线程因为CPython的实现使用了Global Interpereter Lock(GIL),使得python中同一时刻只
- vant文档:Vant 2 - Mobile UI Components built on Vue实现效果: 代码实现:1.nav
- 什么是Flyway?转载:https://blog.waterstrong.me/flyway-in-practice/Flyway is
- SQL Server与Oracle、DB2的优劣对比:1.开放性:SQL Server只能在Windows上运行,没有丝毫的开放性,操作系统
- 学习到的内容:1.一个64位的int类型值,充分利用高32位和低32位,进行相关加减以及从一个64位中拆出高32位和低32位.扩展:如何自己
- 扩展名在写Python程序时我们常见的扩展名是py, pyc,其实还有其他几种扩展名。下面是几种扩展名的用法。pypy就是最基本的源码扩展名
- WebDriver有2个方法执行Java Script脚本。(1)同步执行:execute_script(2)异步执行:execute_as
- 导言GridView是由一组字段(Field)组成的,它们都指定的了来自DataSource中的什么属性需要用到自己的输出呈现中。最简单的字
- 错误信息Attempting to use uninitialized value input_producer/input_produce
- 张同学 10.4号开始发视频,视频的点赞量一直很高,11.17 号的视频达到了顶峰,收获 250w 个赞,之后关注量也开启了暴涨。所以挖掘
- 本文实例讲述了Python实现的质因式分解算法。分享给大家供大家参考,具体如下:本来想实现一个其它的基本数学算法问题,但是发现在实现之前必须
- (高手就不要笑话了^_^)。好了,其他的不说现在就开始:select 子句主要决定了从表中取出的列名,列数以及列的显示顺序等信息,"
- 目录前言示例文件文件编码空值日期错误函数映射方法1:直接使用labmda表达式方法二:使用自定义函数方法三:使用数值字典映射总结前言本文是给
- 刚好前些天有人提到eval()与exec()这两个函数,所以就翻了下Python的文档。这里就来简单说一下这两个函数以及与它们相关的几个函数
- '''数据集:伪造数据集(两个高斯分布混合)数据集长度:1000--------------------------