对于numpy.dot():
一维数组置于矩阵乘法的左部,被视为一个行向量;
一维数组置于矩阵乘法的右部,被视为一个列向量;
矩阵乘法运算结束得到的向量仍是一维数组。
原因:numpy 中的 broadcasting(广播)机制
【例1】一维数组置于矩阵乘法的右部,因为最常见,所以放在第一个来举例
import numpy as np
a = np.random.randint(0, 10, size = (3,6))
print("a")
print(a)
print(a.shape)
b = np.random.randint(0, 10, size = (6,))
print("b")
print(b)
print(b.shape)
c = np.dot(a, b)
print("c")
print(c)
print(c.shape)
输出:
a
[[0 6 3 9 7 9]
[7 2 9 0 2 1]
[3 3 8 2 1 3]]
(3, 6)
b
[8 1 7 8 5 1]
(6,)
c
[143 132 107]
(3,)
【例2】一维数组置于矩阵乘法的左部
import numpy as np
a = np.random.randint(0, 10, size = (3,6))
print("a")
print(a)
print(a.shape)
b = np.random.randint(0, 10, size = (3,))
print("b")
print(b)
print(b.shape)
c = np.dot(b, a)
print("c")
print(c)
print(c.shape)
输出:
a
[[6 6 3 5 2 9]
[7 8 0 3 1 7]
[6 7 6 2 5 4]]
(3, 6)
b
[6 4 6]
(3,)
c
[100 110 54 54 46 106]
(6,)
参考: