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

标签:np.dot
0
投稿

猜你喜欢

  • Go语言HTTPServer开发的六种方式小结

    2023-06-22 21:48:21
  • ASP+SQLServer2000 经验积累

    2008-02-03 15:16:00
  • 怎么样才能让层显示在FLASH之上呢

    2008-03-05 13:32:00
  • 从MySQL 5.5发布看开源数据库新模式

    2010-01-03 19:54:00
  • wordpress网站转移到本地运行测试的方法

    2024-05-11 09:53:39
  • Linux自动备份MySQL数据库脚本代码

    2024-01-24 05:00:12
  • MySQL 创建用户、授权用户、撤销用户权限、更改用户密码、删除用户(实用技巧)

    2024-01-25 06:11:46
  • Go语言提升开发效率的语法糖技巧分享

    2024-05-21 10:22:39
  • 程序员趣味读物 谈谈Unicode编码

    2023-03-19 14:56:13
  • Centos MySQL 5.7安装、升级教程

    2024-01-19 10:24:03
  • Python3操作SQL Server数据库(实例讲解)

    2024-01-24 04:13:21
  • 通过5个例子让你学会Pandas中的字符串过滤

    2022-10-09 03:44:36
  • 让我们一起来学习一下什么是javascript的闭包

    2024-04-17 10:11:43
  • python手机号前7位归属地爬虫代码实例

    2021-01-23 05:20:34
  • getdata table表格数据join mysql方法

    2024-01-25 17:55:08
  • 教你轻松恢复/修复SQL Server的MDF文件

    2024-01-28 17:30:49
  • mysql慢查询日志轮转_MySQL慢查询日志实操

    2024-01-26 05:54:14
  • 超越质检员——看图购beta版的思考

    2009-04-15 12:11:00
  • javascript同页面多次调用弹出层具体实例代码

    2024-04-10 14:02:31
  • 多个应用共存的Django配置方法

    2021-06-28 03:25:37
  • asp之家 网络编程 m.aspxhome.com