PCA machine learning algorithms to resolve --python beginning of

Principal component analysis (PCA) algorithm is an important tool-dimensional visualization of the drop, and today I have also been learning, small programs written in Python, a better understanding of the applications and functions of the algorithm

# 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()



Released six original articles · won praise 19 · views 6545

Guess you like

Origin blog.csdn.net/qq_38833931/article/details/104815417