import numpy as np
第一步:原始值
X1 |
0.9 |
2.4 |
1.2 |
0.5 |
0.3 |
1.8 |
0.5 |
0.3 |
2.5 |
1.3 |
X2 |
1 |
2.6 |
1.7 |
0.7 |
0.7 |
1.4 |
0.6 |
0.6 |
2.6 |
1.1 |
第二步:计算平均值
=1.17,np.mean(x1)=1.1700000000000002
=1.3, np.mean(x2)=1.3
第三步:X1 - ,X2 - ,得到如下表格
X11 |
0.9-=-0.27 |
2.4-=1.23 |
1.2-=0.03 |
0.5-=-0.67 |
0.3-=-0.87 |
1.8-=0.63 |
0.5-=-0.67 |
0.3-=-0.87 |
2.5-=1.33 |
1.3-=0.13 |
X22 |
1-=-0.3 |
2.6-=1.3 |
1.7-=0.4 |
0.7-=0.6 |
0.7-=0.6 |
1.4-=0.1 |
0.6-=-0.7 |
0.6-=-0.7 |
2.6-=1.3 |
1.1-=-0.2 |
x11 = x1 -np.mean(x1)
x11
Out[46]: array([-0.27, 1.23, 0.03, -0.67, -0.87, 0.63, -0.67, -0.87, 1.33, 0.13])
x22 = x2 -np.mean(x2)
x22
Out[48]: array([-0.3, 1.3, 0.4, -0.6, -0.6, 0.1, -0.7, -0.7, 1.3, -0.2])
第四步:计算协方差矩阵(X11,X12)
covx12 = np.cov(x11,x2)
covx12
Out[51]:
array([[ 0.68677778, 0.60666667],
[ 0.60666667, 0.59777778]])
第五步:计算特征值和特征向量covx12
w,v = np.linalg.eig(covx12)
w
Out[53]: array([ 1.25057433, 0.03398123])
v
Out[54]:
array([[ 0.73251454, -0.68075138],
[ 0.68075138, 0.73251454]])
The number w is an eigenvalue of a if there exists a vector v such that dot(a,v) = w * v. Thus, the arrays a, w, and v satisfy the equations dot(a[:,:], v[:,i]) = w[i] * v[:,i]for .
np.dot(covx12,v[:,0])
Out[62]: array([ 0.91606388, 0.8513302 ])
w[0]*v[:,0]
Out[63]: array([ 0.91606388, 0.8513302 ])
np.dot(covx12,v[:,1])
Out[64]: array([-0.02313277, 0.02489174])
w[1]*v[:,1]
Out[65]: array([-0.02313277, 0.02489174])
根据矩阵特征向量大小排序,两个特征向量顺序为 v[:,0],v[:,1].
第六步:降低纬度
X11 |
X12 |
|
|
|
|
|
|
|
|
|
|
(X11,X12)10行*2列 * V[:,0] 2行*1列 得到 = 10行*1列
从而达到由每个2纬向量变为1纬向量的目的