import numpy as np
def main():
X = np.random.randn(10, 3)
A = np.random.randn(3, 3)
sum1 = 0
for i in range(X.shape[0]):
sum1 += np.dot(X[i, :], np.dot(A, X[i, :].T))
sum2 = np.trace(np.dot(A, np.dot(X.T, X)))
print(sum1)
# 10.158513956
print(sum2)
# 10.158513956
if __name__ == '__main__':
main()
令 A 是一个 N×N 的方阵, 且有 N 个线性无关的特征向量 qi(i=1,2,…,N) 。这样 A 可被分解为:
>>> import numpy as np
>>> A = np.random.randn(3, 3)
>>> Lambda, Q = np.linalg.eig(A)
# 其中Lambda是一维向量,由特征值构成
>>> np.dot(Q, np.dot(np.diag(Lambda), np.linalg.inv(Q)))
array([[-0.15446862, -1.57279859, -0.28165496], [-0.99437763, -0.54065794, 0.75029032], [-0.49977911, 1.78752911, -1.17139559]])
>>> A
array([[-0.15446862, -1.57279859, -0.28165496], [-0.99437763, -0.54065794, 0.75029032], [-0.49977911, 1.78752911, -1.17139559]])
>>> [np.linalg.norm(Q[:, i], 2) for i in range(Q.shape[1])]
# 每一个特征向量的二范数,
[0.99999999999999989, 1.0, 1.0]
# 可见,numpy提供的特征分解已为我们做了特征向量的归一化
实对称矩阵不同的特征值对应的特征向量彼此正交(证明见 矩阵理论拾遗)。为了使用numpy线性代数工具箱进行测试,我们首先构造一个对称矩阵:
>>> import numpy as np
>>> A = np.random.randn(3, 3)
>>> A = np.triu(A)
>>> A += (A.T-np.diag(A.diagonal()))
>>> Lambda, Q = np.linalg.eig(A)
>>> np.dot(Q.T, Q)
[[ 1.00000000e+00 -3.88578059e-16 1.11022302e-16] [ -3.88578059e-16 1.00000000e+00 1.11022302e-16] [ 1.11022302e-16 1.11022302e-16 1.00000000e+00]]
实对称矩阵又可被分解为:
import numpy as np
def main():
X = np.random.randn(10, 3)
N = X.shape[0]
C = np.dot(X.T, X)/N
Lambda, Q = np.linalg.eig(C)
print(np.dot(Q, Q.T))
print(np.dot(Q.T, Q))
if __name__ == '__main__':
main()
QTQ=I ,正交矩阵的行列式比为1或-1,
import numpy as np
def main():
X = np.random.randn(10, 3)
C = 0
N = X.shape[0]
for i in range(N):
C += np.dot(X[i][:, np.newaxis], X[i][np.nexaxis, :])
C /= N
C2 = np.dot(X.T, X)/N
print(C==C2)
if __name__ == '__main__':
main()
import numpy as np
def main():
x = np.array([1, 2, 3])
A = np.random.randn(3, 3)
print(np.dot(x, np.dot(A, x)))
s = 0
for i in range(A.shape[0]):
for j in range(A.shape[1]):
s += A[i, j]*x[i]*x[j]
print(s)
if __name__ == '__main__':
main()
列和在重复;
行和在重复;