python 計算矩陣的相關演算法

小黃黃(′ʘ⌄ʘ‵)發表於2020-09-26

矩陣加乘
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)

相關文章