
np.dot()使用方法
·
np.dot()函数主要有两个功能,向量点积和矩阵乘法,这里我就简单列举了三种最常用到的情况
1. np.dot(a, b), 其中a为一维的向量,b为一维的向量,当然这里a和b都是np.ndarray类型的, 此时因为是一维的所以是向量点积。
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 10])
print(np.dot(a, b))
output:
130
[Finished in 0.2s]
2. np.dot(a, b), 其中a为二维矩阵,b为一维向量,这时b会被当做一维矩阵进行计算
import numpy as np
a = np.random.randint(0,10, size = (5,5))
b = np.array([1,2,3,4,5])
print("the shape of a is " + str(a.shape))
print("the shape of b is " + str(b.shape))
print(np.dot(a, b))
output:
the shape of a is (5, 5)
the shape of b is (5,)
[42 85 50 81 76]
[Finished in 0.2s]
这里需要注意的是一维矩阵和一维向量的区别,一维向量的shape是(5, ), 而一维矩阵的shape是(5, 1), 若两个参数a和b都是一维向量则是计算的点积,但是当其中有一个是矩阵时(包括一维矩阵),dot便进行矩阵乘法运算。所以如果是一个向量和一个矩阵相乘,这个向量会自动转换为一维矩阵进行计算。
3. np.dot(a ,b), 其中a和b都是二维矩阵,此时dot就是进行的矩阵乘法运算
import numpy as np
a = np.random.randint(0, 10, size = (5, 5))
b = np.random.randint(0, 10, size = (5, 3))
print("the shape of a is " + str(a.shape))
print("the shape of b is " + str(b.shape))
print(np.dot(a, b))
output:
the shape of a is (5, 5)
the shape of b is (5, 3)
[[ 66 80 98]
[ 53 60 60]
[ 65 84 85]
[ 25 113 101]
[ 42 78 77]]
[Finished in 0.2s]
推荐内容
更多推荐
相关推荐
查看更多
A2A

谷歌开源首个标准智能体交互协议Agent2Agent Protocol(A2A)
adk-python

一款开源、代码优先的Python工具包,用于构建、评估和部署灵活可控的复杂 AI agents
Second-Me

开源 AI 身份系统,通过本地训练和部署,模仿用户思维和学习风格,创建专属AI替身,保护隐私安全。
热门开源项目
活动日历
查看更多
直播时间 2025-04-09 14:34:18

樱花限定季|G-Star校园行&华中师范大学专场
直播时间 2025-04-07 14:51:20

樱花限定季|G-Star校园行&华中农业大学专场
直播时间 2025-03-26 14:30:09

开源工业物联实战!
直播时间 2025-03-25 14:30:17

Heygem.ai数字人超4000颗星火燎原!
直播时间 2025-03-13 18:32:35

全栈自研企业级AI平台:Java核心技术×私有化部署实战
所有评论(0)