import numpy as np
mat = np.array([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3],
[2.3, 2.7],
[2, 1.6],
[1, 1.1],
[1.5, 1.6],
[1.1, 0.9]])
n = np.size(mat,1) #列 2列
l = np.size(mat,0) #行
avg = np.zeros([1,n]) # 行 列
for i in range(n):
avg[0, i] = np.mean(mat[:, i])
for i in range(n):
mat[:,i] = mat[:,i] - avg[0,i]
todd=np.cov(mat,rowvar=False)
val, vec = np.linalg.eig(todd)
print("特征值:", val)
print("特征向量:", vec)
vec = np.transpose(vec)
flag=0
for i in range(n):
if val[i] > flag:
flag = val[i]
index = i
result=np.dot(mat,vec[index])
print("最终结果:",result)
|