# -*- coding:utf-8 _*-
import numpy as np
Data = np.array([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3.0],
[2.3, 2.7],
[2, 1.6],
[1, 1.1],
[1.5, 1.6],
[1.1, 0.9],
])
# x,y平均值
avg_x = np.average(Data[:, 0])
avg_y = np.average(Data[:, 1])
avg = np.array([avg_x, avg_y])
# 每组数据减去对应均值后的数值
DataAdjust = Data-avg
# print(DataAdjust)
# 样本X方差
Variance_x = np.sum(DataAdjust[:,0]*DataAdjust[:, 0])/9
# 样本Y方差
Variance_y = np.sum(DataAdjust[:,1]*DataAdjust[:, 1])/9
# 样本X Y协方差
Cov_xy = np.sum(DataAdjust[:,0]*DataAdjust[:, 1])/9
# 协方差矩阵
C = np.array([[Variance_x, Cov_xy],
[Cov_xy, Variance_y]])
# 特征值,特征向量
eigenvalues, eigenvectors = np.linalg.eig(C)
max_values = np.array(max(eigenvalues))
max_vectors = np.array([eigenvectors[0][1],eigenvectors[1][1]])
FinalData = np.dot(DataAdjust, max_vectors.T).reshape(10, 1)
print(FinalData)
主成分分析(PCA)
最新推荐文章于 2024-09-07 20:08:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)