主成分分析(PCA)算法是降维可视化的重要工具,我今天也进行了学习,Python编写的小程序,更好理解了其算法的应用和功能
# Des:This is a machine learning program!
# Date:2020-3-12
# Author:Gaofeng
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
data = load_iris() #字典形式,方便控制属性和标签
y = data.target #数据集中的标签
x = data.data # 数据集中的属性数据
cpa = PCA(n_components=2) #加载PCA算法,二维化
reduced = cpa.fit_transform(x) #进行数据降维度
print(reduced)
red_x, red_y = [], []
blue_x, blue_y = [], []
green_x, green_y = [], []
for i in range(len(reduced)):
if y[i] == 0:
red_x.append(reduced[i][0])
red_y.append(reduced[i][1])
elif y[i] == 1:
blue_x.append(reduced[i][0])
blue_y.append(reduced[i][1])
else:
green_x.append(reduced[i][0])
green_y.append(reduced[i][1])
plt.scatter(red_x, red_y, c='r', marker='x')
plt.scatter(blue_x, blue_y, c='b', marker='D')
plt.scatter(green_x, green_y, c='g', marker='.')
plt.xlabel("Data_x")
plt.ylabel("Data_y")
plt.title("PCA algorithm")
plt._show()