下面是python2版本的程序:
对鸢尾花数据进行主成分分析法的操作,并画图:
import matplotlib.pyplot as plt #加载matplotlib用于数据的可视化
from sklearn.decomposition import PCA #加载pca算法包
from sklearn.datasets import load_iris
import numpy as np #加下面两句话实际上是,设置显示阈值,
np.set_printoptions(threshold = 1e6) #如何不加, python print只能显示首尾30个数据,其他是省略号
data=load_iris()
y=data.target
x=data.data
pca=PCA(n_components=2) #加载PCA算法,设置降维后主成分数目为2
reduced_x=pca.fit_transform(x) #对样本进行降维
red_x,red_y=[],[]
blue_x,blue_y=[],[]
green_x,green_y=[],[]
if __name__ == "__main__":
for i in range(len(reduced_x)):
if y[i] ==0:
red_x.append(reduced_x[i][0])
red_y.append(reduced_x[i][1])
elif y[i]==1:
blue_x.append(reduced_x[i][0])
blue_y.append(reduced_x[i][1])
else:
green_x.append(reduced_x[i][0])
green_y.append(reduced_x[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.show()