#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()# 输出图像
View Code

相关文章:

  • 2021-04-08
  • 2022-12-23
  • 2021-12-07
  • 2021-05-20
  • 2021-06-05
  • 2022-12-23
  • 2021-12-31
  • 2022-01-18
猜你喜欢
  • 2021-11-22
  • 2021-11-23
  • 2021-12-22
  • 2022-01-15
  • 2021-08-12
  • 2021-08-19
  • 2021-11-11
相关资源
相似解决方案