首先,導入函數包:
import numpy as np
1.np.multiply()函數:
數組:(點對點)對應位置元素相乘
矩陣:對應位置元素相乘
示例:
A = np.array([[1,2],[3,4]])
B = np.array([[1,3],[2,4]])
A_mat = np.mat(A)
B_mat = np.mat(B)
A_B_multiply = np.multiply(A, B)
print(A_B_multiply)
'''
[[1,6],
[6,16]]
'''
A_B_mat_multiply = np.multiply(A.mat, B.mat)
print(A_B_mat_multiply)
'''
[[1,6],
[6,16]]
'''
2.np.dot()函數:
數組:(矩陣乘法)行對列,對應元素乘積后相加求和
矩陣:行對列,對應元素乘積后相加求和
示例:
A = np.array([[1,2],[3,4]])
B = np.array([[1,3],[2,4]])
A_mat = np.mat(A)
B_mat = np.mat(B)
A_B_dot = np.dot(A, B)
print(A_B_dot)
'''
[[5,11],
[11,25]]
'''
A_B_mat_dot = dot(A.mat, B.mat)
print(A_B_mat_dot)
'''
[[5,11],
[11,25]]
'''
3.* :
數組:對應位置元素相乘
矩陣:行對列,對應元素乘積后相加求和
示例:
A = np.array([[1,2],[3,4]])
B = np.array([[1,3],[2,4]])
A_mat = np.mat(A)
B_mat = np.mat(B)
print(A*B)
'''
[[1,6],
[6,16]]
'''
pritn((np.mat(A))*(np.mat(B)))
'''
[[5,11],
[11,25]]
'''
總結:
數組
矩陣
np.multiply()
點對點相乘
點對點相乘
np.dot()
矩陣乘法
矩陣乘法
*
點對點相乘
矩陣乘法
標簽:mat,python,矩陣,np,multiply,array,numpy,dot