a = np.array([[1,2,3,4],[5,6,7,8]])
a
# 显示以下结果:
# array([[1, 2, 3, 4],
# [5, 6, 7, 8]])
a.shape
# 显示以下结果:
# (2, 4)
b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
b
# 显示以下结果:
# array([[ 1, 2, 3],
# [ 4, 5, 6],
# [ 7, 8, 9],
# [10, 11, 12]])
b.shape
# 显示以下结果:
# (4, 3)
c = np.matmul(a, b)
c
# 显示以下结果:
# array([[ 70, 80, 90],
# [158, 184, 210]])
c.shape
# 显示以下结果:
# (2, 3)
m = np.array([[1,2,3],[4,5,6]])
m
# 显示以下结果:
# array([[1, 2, 3],
# [4, 5, 6]])
n = m * 0.25
n
# 显示以下结果:
# array([[ 0.25, 0.5 , 0.75],
# [ 1. , 1.25, 1.5 ]])
m * n
# 显示以下结果:
# array([[ 0.25, 1. , 2.25],
# [ 4. , 6.25, 9. ]])
np.multiply(m, n) # 相当于 m * n
# 显示以下结果:
# array([[ 0.25, 1. , 2.25],
# [ 4. , 6.25, 9. ]])
a = np.array([[1,2],[3,4]])
a
# 显示以下结果:
# array([[1, 2],
# [3, 4]])
np.dot(a,a)
# 显示以下结果:为正常的矩阵乘法运算
# array([[ 7, 10],
# [15, 22]])
a.dot(a) # you can call你可以直接对 `ndarray` 调用 `dot`
# 显示以下结果:
# array([[ 7, 10],
# [15, 22]])
np.matmul(a,a)
# 当两个矩阵的尺寸满足矩阵运算的条件时,np.dot和np.matmul的结果一致
# array([[ 7, 10],
# [15, 22]])
np.dot()函数的官方用法:
a = [1,2,3]
b = [2,2,2]
c = np.dot(a,b)
print(c) # 输出:12
a = [[1,2],[3,4]]
b = [[1,2],[3,4]]
c = np.dot(a,b)
print(c)
# 输出:
# [[ 7 10]
# [15 22]]
print(np.dot(3,4)) # 12
a = [[1,2,3],[2,3,4]]
b = [1,2,3]
c = np.dot(a,b)
print(c) # 输出:[14 20]
c中的元素计算过程:
c[0] = 1x1+2x2+3x3 = 14
c[1] = 2x1+3x2+4x3 = 20
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
参考连接:
[1] https://www.jb51.net/article/208029.htm
[2] https://numpy.org/doc/stable/reference/generated/numpy.dot.html