#Iris花数据集分析 print(__doc__) #输出文件开头注释的内容 import matplotlib.pyplot as plt # 导入matplotlib画图 from mpl_toolkits.mplot3d import Axes3D # 导入mpl_toolkits画3D图像 from sklearn import datasets # 导入sklearn自带的训练集 from sklearn.decomposition import PCA # 导入特征降维的PCA主成分分析法 iris = datasets.load_iris() # 导入iris花数据集进iris变量中 X = iris.data[:, :2] # 导入图像数据给X变量,只使用头两个特征向量 y = iris.target # 导入图像标签给Y,即图像的结果,如1.2.3...9 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 # 设置下,y的最大值和最小值 plt.figure(2, figsize=(8, 6)) plt.clf() #cla() Clear axis即清除当前图形中的当前活动轴。其他轴不受影响。 #clf() Clear figure清除所有轴,但是窗口打开,这样它可以被重复使用。 #close() Close a figure window plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired) # 设置输出图像为散点图 plt.xlabel('Sepal length') plt.ylabel('Sepal width') # 设置输出图像的X轴和Y轴的标签 plt.xlim(x_min, x_max) #设置x轴刻度范围 plt.ylim(y_min, y_max) plt.xticks(()) #设置x轴刻度 plt.yticks(()) fig = plt.figure(1, figsize=(8, 6)) ax = Axes3D(fig, elev=-150, azim=110) # 设置3D图像 X_reduced = PCA(n_components=3).fit_transform(iris.data) #用PCA给特征向量降维 ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=y, cmap=plt.cm.Paired) # 在3D图像中显示散点信息 ax.set_title("First three PCA directions") # 设置3D图像标题 ax.set_xlabel("1st eigenvector") ax.w_xaxis.set_ticklabels([]) #有了这行代码,X轴的坐标刻度就没有了 ax.set_ylabel("2nd eigenvector") ax.w_yaxis.set_ticklabels([]) #有了这行代码,Y轴的坐标刻度就没有了 ax.set_zlabel("3rd eigenvector") ax.w_zaxis.set_ticklabels([]) #有了这行代码,Z轴的坐标刻度就没有了 plt.show()# 输出图像
相关文章: