numpy中数组array与矩阵matrix的乘法运算的不同,主要操作为multiply、dot、*三种。
from numpy import *
# array环境下
a =[[5,6],[7,8]]
b =[[1,2],[3,4]]
a = array(a)
b = array(b)
c = multiply(a,b)
d = dot(a,b)
e = a*b
print('a = ',a)
print('b = ',b)
print('c = multiply(a,b) = ',c)
print('d = dot(a,b) = ',d)
print('e = a*b = ',e)
a = [[5 6]
[7 8]]
b = [[1 2]
[3 4]]
c = multiply(a,b) = [[ 5 12]
[21 32]]
d = dot(a,b) = [[23 34]
[31 46]]
e = a*b = [[ 5 12]
[21 32]]
# matrix环境下
a =[[5,6],[7,8]]
b =[[1,2],[3,4]]
a = mat(a)
b = mat(b)
c = multiply(a,b)
d = dot(a,b)
e = a*b
print('a = ',a)
print('b = ',b)
print('c = multiply(a,b) = ',c)
print('d = dot(a,b) = ',d)
print('e = a*b = ',e)
a = [[5 6]
[7 8]]
b = [[1 2]
[3 4]]
c = multiply(a,b) = [[ 5 12]
[21 32]]
d = dot(a,b) = [[23 34]
[31 46]]
e = a*b = [[23 34]
[31 46]]
小结
数组与矩阵计算,multiply与dot运算相同。
multiply为对应位置相乘,dot为矩阵相乘。
点乘意思不同,array的点乘为对应位置相乘、matrix的点乘为矩阵乘法运算。