一、推导
二、PCA降维计算步骤
二、代码
将sklearn中的150个四维数据降到二维(依照PCA步骤),并进行可视化操作
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
def pca(X, k): # 降到k维
# 1.数据的数量及维数 shape(150, 4)
numbers, dimension = X.shape
# 2.零均值化
mean = np.array([np.mean(X[:, i]) for i in range(dimension)])
norm_X = X - mean
# 3.求协方差矩阵
cov_matrix = np.dot(norm_X.T, norm_X) / numbers
# 4.求协方差矩阵的特征值和特征向量
eig_val, eig_vec = np.linalg.eig(cov_matrix)
# 5.1.特征向量按特征值大小排序
eig_pairs = [(np.abs(eig_val[i]), eig_vec[:, i]) for i in range(dimension)]
eig_pairs.sort(reverse=True)
# 5.2.取前k行组成P
feature = np.array([ele[1] for ele in eig_pairs[:k]])
# Y=PX即为降维后的数据
Y = np.dot(norm_X, feature.T)
return Y
X = datasets.load_iris().data
# 将4维数据X映射到2维空间
X_trans = pca(X, 2)
x1 = X_trans[:, 0]
x2 = X_trans[:, 1]
#绘图
plt.scatter(x1[:50], x2[:50], c='b')
plt.scatter(x1[50:100], x2[50:100], c='g')
plt.scatter(x1[100:150], x2[100:150], c='y')
plt.legend()
plt.xlabel('pc1')
plt.ylabel('pc2')
plt.show()
二、效果图