python 计算矩阵的相关算法

矩阵加乘
import numpy as np
np.random.seed(1010)
A = np.random.normal(-1,1,size=12).reshape(3,4)
B = np.random.normal(1,1,size=12).reshape(4,3)
2A+3B.T
C = np.dot(A,B)
print(A,"\n",B,"\n",C)

行列式,维数
print (np.linalg.det©)#行列式
CI=np.linalg.inv©#逆矩阵
print (CI.dot©)
print (np.round(CI.dot©,14))
K=np.kron(A,B)#Kronecker乘积
K.shape #维数
M=np.dot(np.dot(B,np.linalg.inv(
np.dot(B.T,B))),B.T)
print(M,"\n",np.round(np.dot(M,M)-M,14))

相关矩阵级特征值
np.random.seed(1010)
X=np.random.normal(25,4,size=100)
X.shape=25,4
XC=np.corrcoef(X.T)
w,v=np.linalg.eig(XC)#特征值及向量
np.round(v.T.dot(v),14)
print(XC)
print(w,"\n",v)
print (np.sum(w))
print (np.sum(np.diag(XC)))
x矩阵奇异值分解
u,d,v=np.linalg.svd(X,full_matrices=False)
print (u,"\n",d,"\n",v)
np.round(np.dot(u.T,u),14)
np.round(np.dot(v.T,v),14)
np.round(np.dot(np.dot(u,np.diag(d)),v)-X)
eva,eve=np.linalg.eig(np.dot(X.T,X))
print(np.sqrt(eva),d)

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页