numpy之矩阵基本操作(加减乘,转置,求逆)

import numpy as np

#矩阵创建
matrix1 = np.mat(“1 2 3;4 5 6;7 8 9”)
print(matrix1,type(matrix1))

matrix2 = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
print(matrix2)

arr1 = np.eye(3)
arr2 = 3*np.eye(3)
matrix3 = np.bmat(‘arr1 arr2;arr2 arr1’)
print(matrix3)

#2 矩阵运算
#矩阵加法
matr1 = matrix1 + matrix2
matr2 = matrix1 + matrix2

print(matr1)
print(matr2)

#矩阵乘法
print(2matr1)
print(matrix1
matrix2) #矩阵相乘
print(np.multiply(matr1,matr2)) #对应元素相乘

#矩阵转置
print(matrix2.T)

#矩阵求逆
print(matrix2.I)

你可能感兴趣的:(数据分析)