1. np.multiply()函数
数组和矩阵对应位置相乘,输出与相乘数组/矩阵的大小一致
1.1 数组场景
import numpy as np
a = np.array([[1, 2],
[3,4]])
b = np.array([[5, 6],
[7, 8]])
c = np.multiply(a, b)
print (c)
输出:
[[ 5 12] #对应元素相乘
[21 32]]
1.2矩阵场景
a = np.array([[1, 2],
[3,4]])
a = np.mat(a)
b = np.array([[5, 6],
[7, 8]])
b = np.mat(b)
c = np.multiply(a, b)
print (c)
输出:
[[ 5 12] #对应元素相乘
[21 32]]
2.np.dot()函数:
对于秩为1的数组,执行对应位置相乘,然后再相加
对于秩不为1的二维数组,执行矩阵乘法运算
2.1数组场景
2.1.1数组秩为1
a = np.array([1,2])
b = np.array([3,4])
c = np.dot(a,b)
print (c)
输出:
11 #对应元素相乘再求和
2.1.2数组秩不为1
a = np.array([[1, 2],
[3,4]])
b = np.array([[5, 6],
[7, 8]])
c = np.dot(a, b)
print (c)
输出:
[[19 22] #数组执行矩阵相乘运算
[43 50]]
2.2矩阵场景
a = np.array([[1, 2],
[3,4]])
a = np.mat(a)
b = np.array([[5, 6],
[7, 8]])
b = np.mat(b)
c = np.dot(a, b)
print (c)
输出:
[[19 22] #执行矩阵乘法运算
[43 50]]
3.(*)运算
对数组执行对应位置相乘
对矩阵执行矩阵乘法运算
3.1数组场景
a = np.array([[1, 2],
[3,4]])
b = np.array([[5, 6],
[7, 8]])
c = a * b
print (c)
输出:
[[ 5 12] #对应元素相乘
[21 32]]
3.2矩阵场景
a = np.array([[1, 2],
[3,4]])
a = np.mat(a)
b = np.array([[5, 6],
[7, 8]])
b = np.mat(b)
c = a * b
print (c)
输出:
[[19 22] #执行矩阵乘法运算
[43 50]]