import numpy as np
x=np.array([2.5,0.5,2.2,1.9,3.1,2.3,2,1,1.5,1.1])
y=np.array([2.4,0.7,2.9,2.2,3.0,2.7,1.6,1.1,1.6,0.9])
sum1=0
sum2=0
for i in range(0,10):
sum1+=x[i]
sum2+=y[i]
x_ave=sum1/10
y_ave=sum2/10
print(x_ave)
print(y_ave)
1.81
1.9099999999999997
xx=np.zeros(10)
yy=np.zeros(10)
for i in range(0,10):
xx[i]=x[i]-x_ave
yy[i]=y[i]-y_ave
print(xx)
print(yy)
data=np.array([xx,yy])
print(data)
data1=np.transpose(data)
print(data1)
[ 0.69 -1.31 0.39 0.09 1.29 0.49 0.19 -0.81 -0.31 -0.71]
[ 0.49 -1.21 0.99 0.29 1.09 0.79 -0.31 -0.81 -0.31 -1.01]
[[ 0.69 -1.31 0.39 0.09 1.29 0.49 0.19 -0.81 -0.31 -0.71]
[ 0.49 -1.21 0.99 0.29 1.09 0.79 -0.31 -0.81 -0.31 -1.01]]
[[ 0.69 0.49]
[-1.31 -1.21]
[ 0.39 0.99]
[ 0.09 0.29]
[ 1.29 1.09]
[ 0.49 0.79]
[ 0.19 -0.31]
[-0.81 -0.81]
[-0.31 -0.31]
[-0.71 -1.01]]
cov1=0
cov2=0
cov3=0
for i in range(0,10):
cov1+=xx[i]*xx[i]
cov2+=xx[i]*yy[i]
cov3+=yy[i]*yy[i]
cov_xx=cov1/9
cov_xy=cov2/9
cov_yx=cov2/9
cov_yy=cov3/9
print(cov_xx)
print(cov_xy)
print(cov_yx)
print(cov_yy)
0.6165555555555556
0.6154444444444445
0.6154444444444445
0.7165555555555556
cov=np.array([[cov_xx,cov_xy],
[cov_yx,cov_yy]])
print(cov)
[[0.61655556 0.61544444]
[0.61544444 0.71655556]]
value,vector = np.linalg.eig(cov)
print("特征值:", value)
print("特征向量:", vector)
特征值: [0.0490834 1.28402771]
特征向量: [[-0.73517866 -0.6778734 ]
[ 0.6778734 -0.73517866]]
v=np.array([[-0.6778734],
[-0.73517866]])
fd=np.zeros(100)
fd=np.dot(data1,v)
print(fd)
[[-0.82797019]
[ 1.77758033]
[-0.9921975 ]
[-0.27421042]
[-1.67580143]
[-0.91294911]
[ 0.09910944]
[ 1.14457217]
[ 0.43804614]
[ 1.22382056]]