矩阵特征向量与奇异值分解
特征向量-直方图
1from scipy.linalg import eig # 矩阵分解
2import numpy as np # 生成矩阵
3import matplotlib.pyplot as plt # 绘图
1A = [[1, 2],#生成一个2*2的矩阵
2 [2, 1]]
3A
[[1, 2], [2, 1]]
1
2# 特征分解
3evals, evecs = eig(A) #求A的特征值(evals)和特征向量(evecs)
[[ 0.70710678 -0.70710678] [ 0.70710678 0.70710678]]
1evecs = evecs[:, 0], evecs[:, 1]
2print(evecs)
(array([0.70710678, 0.70710678]), array([-0.70710678, 0.70710678]))
1fig, ax = plt.subplots()
2for spine in ['left', 'bottom']:#让在左下角的坐标轴经过原点
3 ax.spines[spine].set_position('zero')
4 ax.grid(alpha=0.4)
5xmin, xmax = -3, 3
6ymin, ymax = -3, 3
7ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
8for v in evecs:
9 ax.annotate(s="", xy=v, xytext=(0, 0),
10 arrowprops=dict(facecolor='blue',
11 shrink=0,
12 alpha=0.6,
13 width=0.5))
14x = np.linspace(xmin, xmax, 3)#在指定的间隔内返回均匀间隔的数字
15for v in evecs:
16 a = v[1] / v[0] #沿特征向量方向的单位向量
17 ax.plot(x, a * x, 'r-', lw=0.4)# 参数 lw 表示图线的粗细
18plt.show()
奇异值分解
1import numpy as np
2import matplotlib.pyplot as plt
1words = ["books","dad","stock","value","singular","estate","decomposition"]
1X=np.array([[0,2,1,0,0,0,0],[2,0,0,1,0,1,0],[1,0,0,0,0,0,1],[0,0,1,0,0,0,0],[0,1,0,0,0,0,0],[0,0,0,1,1,0,1],[0,1,0,0,1,0,0],[0,0,0,0,1,1,1]])
1X
array([[0, 2, 1, 0, 0, 0, 0], [2, 0, 0, 1, 0, 1, 0], [1, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 1], [0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1]])
1U,s,Vh=np.linalg.svd(X)
1print("U=",U)
U= [[-1.87135757e-01 -7.93624528e-01 2.45011855e-01 -2.05404352e-01 -3.88578059e-16 5.75779114e-16 -2.57394431e-01 -4.08248290e-01] [-6.92896814e-01 2.88368077e-01 5.67788037e-01 2.22142537e-01 2.54000254e-01 -6.37019839e-16 -2.21623012e-02 2.05865892e-17] [-3.53233681e-01 1.22606651e-01 3.49203461e-02 -4.51735990e-01 -7.62000762e-01 1.27403968e-15 2.72513448e-01 3.80488702e-17] [-2.61369658e-02 -1.33189110e-01 7.51079037e-02 -6.44727454e-01 5.08000508e-01 1.77635684e-15 3.68146235e-01 4.08248290e-01] [-8.04993957e-02 -3.30217709e-01 8.49519758e-02 2.19661551e-01 -2.54000254e-01 -4.81127681e-16 -3.12770333e-01 8.16496581e-01] [-3.95029694e-01 1.56123876e-02 -5.28290830e-01 -6.82340484e-02 1.27000127e-01 -7.07106781e-01 -2.09360158e-01 1.55512464e-17] [-2.02089013e-01 -3.80395849e-01 -2.12899198e-01 4.80790894e-01 8.04483689e-16 -1.60632798e-15 7.33466480e-01 1.76241226e-16] [-3.95029694e-01 1.56123876e-02 -5.28290830e-01 -6.82340484e-02 1.27000127e-01 7.07106781e-01 -2.09360158e-01 -1.23226632e-16]]
1print("s=",s)
s= [2.85653844 2.63792139 2.06449303 1.14829917 1. 1. 0.54848559]
1print("Vh",Vh)
Vh [[-6.08788345e-01 -2.29949618e-01 -7.46612474e-02 -3.80854846e-01 -3.47325416e-01 -3.80854846e-01 -4.00237243e-01] [ 2.65111314e-01 -8.71088358e-01 -3.51342402e-01 1.15234846e-01 -1.32365989e-01 1.15234846e-01 5.83153945e-02] [ 5.66965547e-01 1.75382762e-01 1.55059743e-01 1.91316736e-02 -6.14911671e-01 1.91316736e-02 -4.94872736e-01] [-6.48865369e-03 2.52237176e-01 -7.40339999e-01 1.34031699e-01 2.99854608e-01 1.34031699e-01 -5.12239408e-01] [-2.54000254e-01 -2.54000254e-01 5.08000508e-01 3.81000381e-01 2.54000254e-01 3.81000381e-01 -5.08000508e-01] [ 0.00000000e+00 -7.68640544e-16 2.33583082e-15 -7.07106781e-01 -1.21802199e-15 7.07106781e-01 1.91457709e-15] [ 4.16034348e-01 -1.71550021e-01 2.01922906e-01 -4.22112199e-01 5.73845817e-01 -4.22112199e-01 -2.66564648e-01]]
1plt.axis([-0.8,0.2,-0.8,0.8])
2for i in range(len(words)):
3 plt.text(U[i,0],U[i,1],words[i])
4plt.show()