1 数学对象
- 标量
import numpy as np
#标量只是一个单一的数字
scalar_value = 18
print(scalar_value)
注意:
print(scalar_value,scalar_value.shape())#对int类型的数不能通过shape得到型,应该转化为numpy中的数组array
- 向量
#向量是一个有序的数字数组
vector_value = [1,2,3] #这是一个列表
vector_np = np.array(vector_value) #转化为np中的数组
print(vector_np,vector_np.shape) #shape显示为一维数组,其实这既不能算行向量,也不能算列向量
3. 矩阵
#矩阵是一个有序的二维数组,它有两个索引。第一个指向该行,第二个指向该列
matrix_list = [[1,2,3],[4,5,6]]
matrix_np = np.array(matrix_list)
print("matrix_list=",matrix_list,"\n","matrix_np=\n",matrix_np,\
"\n","matrix_np.shape=",matrix_np.shape)
- 行向量的矩阵表示
#行向量的矩阵表示
vector_row = np.array([[1,2,3]])
print(vector_row,"shape=",vector_row.shape)
5. 列向量的矩阵表示
#列向量的矩阵表示
vector_column = np.array([[4],[5],[6]])
print(vector_column,"shape=",vector_column.shape)
2 矩阵运算
- 矩阵 * 标量
#矩阵与标量运算
matrix_a = np.array([[1,2,3],[4,5,6]])
print(matrix_a,"shape=",matrix_a.shape)
#矩阵 * 标量
matrix_b = matrix_a * 2
print(matrix_b,"shape=",matrix_a.shape)
- 矩阵 + 标量
#矩阵 + 标量
matrix_c = matrix_a + 2
print(matrix_c,"shape=",matrix_c.shape)
- 矩阵 + 矩阵
#矩阵 + 矩阵
matrix_a = np.array([[1,2,3],
[4,5,6]]) #2行3列
matrix_b = np.array([[-1,-2,-3],
[-4,5,-6]]) #2行3列
matrix_a + matrix_b
- 矩阵 * 矩阵(点积)
#矩阵 * 矩阵(点积),对应位置数字相乘,两个矩阵行列数必须相等
matrix_a = np.array([[1,2,3],
[4,5,6]]) #2行3列
matrix_b = np.array([[-1,-2,-3],
[-4,5,-6]]) #2行3列
print(matrix_a * matrix_b) #点积1
print(np.multiply(matrix_a,matrix_b)) #点积2,与点积1等价
- 叉乘
#矩阵--矩阵乘法(叉乘)
# m * n 叉乘 n * k => m * k
#如果第一个矩阵列的数量与第二个矩阵行的数量相匹配,才能将矩阵相乘
#结果将是一个矩阵,它具有与第一个矩阵相同的行数和与第二个矩阵相同的列数
matrix_a = np.array([[1,2,3],
[4,5,6]])#2行3列
matrix_b = np.array([[1,2,3,4],
[2,1,2,0],
[3,4,1,2]])#3行4列
np.matmul(matrix_a,matrix_b) #结果是2行4列
- 矩阵转置
matrix_a = np.array([[1,2,3],
[4,5,6]])
print(matrix_a,"shape=",matrix_a.shape,"\n",matrix_a.T,".Tshape=",matrix_a.T.shape)
行列转置:
#行列转置
vector_row = np.array([[1,2,3]])
print(vector_row,"shape=",vector_row.shape,"\n",vector_row.T,"shape=",vector_row.T.shape)
reshape 与 .T转置 等价 :
#reshape 与 .T转置 等价
vector_row = np.array([[1,2,3]])
vector_column = vector_row.reshape(3,1)
print(vector_row,"shape=",vector_row.shape,"\n",vector_column,"shape=",vector_column.shape)