实验2.1要求:

  1. 参考任务1.2有关鸢尾花数据的相关介绍,了解数据各列的含义。
  2. 对每种类型的鸢尾花的四个属性进行可视化绘图,观察各属性在不同类型间的相关性及区分度。

注:

  1. 数据可以选择使用任务1.2步骤3清理之后的数据,也可使用sklearn包自带的载入数据方式,参考代码如下:
    from sklearn import datasets
    iris = datasets.load_iris()
  2. 画图可以使用matplotlib包。
  3. 可依次选取每项特征绘制,例如,以萼片长度为x轴、萼片宽度为y轴作散点图进行观察。完成后继续观察下一组特征。
  4. 结果可参见下图示例。

image.png

2.1上机实现:

导入需要用到的包

  1. import pandas as pd
  2. import numpy as np
  3. np.seterr(divide='ignore', invalid='ignore')
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D

导入数据集

  1. file_path = r"D:\DataAnalysis\data2.csv" # r对路径进行转义,windows需要
  2. data = pd.read_csv(file_path, header=0) # header=0表示第一行是表头,就自动去除了
  3. data

data2.csv

Unnamed: 0 Sepal Length Sepal Width Petal Length Petal Width Class Normalization
0 1 5.1 3.5 1.4 0.2 0.0 0.800285
1 2 4.9 3.0 1.4 0.2 0.0 0.932836
2 4 4.7 3.2 1.3 0.2 0.0 0.585938
3 5 4.6 3.1 1.5 0.2 0.0 0.520833
4 6 5.0 3.6 1.4 0.2 0.0 0.000000
143 151 6.7 3.3 5.7 2.5 2.0 0.520833
144 152 6.7 3.0 5.2 2.3 2.0 0.000000
145 153 6.3 2.5 5.0 1.9 2.0 0.807418
146 155 6.2 3.4 5.4 2.3 2.0 0.925373
147 156 5.9 3.0 5.1 1.8 2.0 0.585938

148 rows × 7 columns

绘制散点图

预处理

  1. df = data
  2. from pylab import mpl
  3. mpl.rcParams['font.sans-serif'] = ['STZhongsong'] # 指定默认字体:解决plot不能显示中文问题
  4. mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
  5. plt.style.use('fivethirtyeight')
  6. Sepal_Length_Setosa = df.loc[0:48,'Sepal Length']
  7. Sepal_Length_Versicolour= df.loc[49:96,'Sepal Length']
  8. Sepal_Length_Virginica= df.loc[97:142,'Sepal Length']
  9. Sepal_Width_Setosa= df.loc[0:48,'Sepal Width']
  10. Sepal_Width__Versicolour= df.loc[49:96,'Sepal Width']
  11. Sepal_Width__Virginica= df.loc[97:142,'Sepal Width']
  12. Petal_Length_Setosa = df.loc[0:48,'Petal Length']
  13. Petal_Length_Versicolour= df.loc[49:96,'Petal Length']
  14. Petal_Length_Virginica= df.loc[97:142,'Petal Length']
  15. Petal_Width_Setosa = df.loc[0:48,'Petal Width']
  16. Petal_Width_Versicolour= df.loc[49:96,'Petal Width']
  17. Petal_Width_Virginica= df.loc[97:142,'Petal Width']

Sepal Length和Sepal Width的关系

  1. plt.scatter(Sepal_Length_Setosa,Sepal_Width_Setosa,label = 'Setosa',c='r')
  2. plt.scatter(Sepal_Length_Versicolour,Sepal_Width__Versicolour,label = 'Versicolour',c='y')
  3. plt.scatter(Sepal_Length_Virginica,Sepal_Width__Virginica,label = 'Virginica',c='g')
  4. plt.title('Sepal Length和Sepal Width的关系')
  5. plt.xlabel('Sepal Length')
  6. plt.ylabel('Sepal Width')
  7. plt.legend()
  8. plt.show()

Sepal Length和Petal Length的关系

  1. plt.scatter(Sepal_Length_Setosa,Petal_Length_Setosa,label = 'Setosa',c='r')
  2. plt.scatter(Sepal_Length_Versicolour,Petal_Length_Versicolour,label = 'Versicolour',c='y')
  3. plt.scatter(Sepal_Length_Virginica,Petal_Length_Virginica,label = 'Virginica',c='g')
  4. plt.title('Sepal Length和Petal Length的关系')
  5. plt.xlabel('Sepal Length')
  6. plt.ylabel('Petal Length')
  7. plt.legend()
  8. plt.show()

Sepal Length和Petal Width的关系

  1. plt.scatter(Sepal_Length_Setosa,Petal_Width_Setosa,label = 'Setosa',c='r')
  2. plt.scatter(Sepal_Length_Versicolour,Petal_Width_Versicolour,label = 'Versicolour',c='y')
  3. plt.scatter(Sepal_Length_Virginica,Petal_Width_Virginica,label = 'Virginica',c='g')
  4. plt.title('Sepal Length和Petal Width的关系')
  5. plt.xlabel('Sepal Length')
  6. plt.ylabel('Petal Width')
  7. plt.legend()
  8. plt.show()
  9. #

Sepal Width和Petal Length的关系

  1. plt.scatter(Sepal_Width_Setosa,Petal_Length_Setosa,label = 'Setosa',c='r')
  2. plt.scatter(Sepal_Width__Versicolour,Petal_Length_Versicolour,label = 'Versicolour',c='y')
  3. plt.scatter(Sepal_Width__Virginica,Petal_Length_Virginica,label = 'Virginica',c='g')
  4. plt.title('Sepal Width和Petal Length的关系')
  5. plt.xlabel('Sepal Width')
  6. plt.ylabel('Petal Length')
  7. plt.legend()
  8. plt.show()

Sepal Width和Petal Width的关系

  1. plt.scatter(Sepal_Width_Setosa,Petal_Width_Setosa,label = 'Setosa',c='r')
  2. plt.scatter(Sepal_Width__Versicolour,Petal_Width_Versicolour,label = 'Versicolour',c='y')
  3. plt.scatter(Sepal_Width__Virginica,Petal_Width_Virginica,label = 'Virginica',c='g')
  4. plt.title('Sepal Width和Petal Width的关系')
  5. plt.xlabel('Sepal Width')
  6. plt.ylabel('Petal Width')
  7. plt.legend()
  8. plt.show()

绘制三维图

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. # 随机种子
  5. np.random.seed(1)
  6. def randrange(n, vmin, vmax):
  7. '''
  8. 使数据分布均匀(vmin, vmax).
  9. '''
  10. return (vmax - vmin)*np.random.rand(n) + vmin
  11. fig = plt.figure()
  12. ax = fig.add_subplot(111, projection='3d') # 可进行多图绘制
  13. n = 500
  14. # 对于每一组样式和范围设置,在由x在[23,32]、y在[0,100]、
  15. # z在[zlow,zhigh]中定义的框中绘制n个随机点
  16. '''
  17. for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
  18. xs = randrange(n, 23, 32)
  19. ys = randrange(n, 0, 100)
  20. zs = randrange(n, zlow, zhigh)
  21. ax.scatter(xs, ys, zs, marker=m) # 绘图
  22. '''
  23. xs = data['Petal Length']
  24. ys = data['Petal Width']
  25. zs = data['Sepal Length']
  26. ax.scatter(xs,ys,zs)
  27. # X、Y、Z的标签
  28. ax.set_xlabel('Petal Length')
  29. ax.set_ylabel('Petal Width')
  30. ax.set_zlabel('Sepal Length')
  31. plt.show()

实验2 鸢尾花数据可视化 - 图2image.png

  1. # 随机种子
  2. np.random.seed(1)
  3. def randrange(n, vmin, vmax):
  4. '''
  5. 使数据分布均匀(vmin, vmax).
  6. '''
  7. return (vmax - vmin)*np.random.rand(n) + vmin
  8. fig = plt.figure()
  9. ax = fig.add_subplot(111, projection='3d') # 可进行多图绘制
  10. n = 500
  11. # 对于每一组样式和范围设置,在由x在[23,32]、y在[0,100]、
  12. # z在[zlow,zhigh]中定义的框中绘制n个随机点
  13. '''
  14. for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
  15. xs = randrange(n, 23, 32)
  16. ys = randrange(n, 0, 100)
  17. zs = randrange(n, zlow, zhigh)
  18. ax.scatter(xs, ys, zs, marker=m) # 绘图
  19. '''
  20. xs = data['Sepal Length']
  21. ys = data['Sepal Width']
  22. zs = data['Petal Width']
  23. ax.scatter(xs,ys,zs)
  24. # X、Y、Z的标签
  25. ax.set_xlabel('Sepal Length')
  26. ax.set_ylabel('Sepal Width')
  27. ax.set_zlabel('Petal Width')
  28. plt.show()

image.png

PCA主成分分析实现

  1. '''
  2. 以下为对鸢尾花四维数据进行主成分分析将四维降为三维后进行可视化的实现代码
  3. '''
  4. fig = plt.figure(1, figsize=(14, 12))
  5. ax = plt.axes(projection='3d')
  6. X = PCA(n_components=3).fit_transform(iris.data)
  7. # n_components 是 PCA 算法中所要保留的主成分个数 n, 即保留下来的特征个数 n, 这里四维降三维, 所以 n_components=3
  8. # fit_transform 对数据先拟合 fit, 找到数据的整体指标, 如均值、方差、最大值最小值等, 然后对数据集进行转换 transform, 从而实现数据的标准化、归一化操作
  9. ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=iris.target, cmap=plt.cm.Set1, edgecolor='w', s=40)
  10. # 绘制坐标轴
  11. ax.set_xlabel('1st component')
  12. ax.set_ylabel('2nd component')
  13. ax.set_zlabel('3rd component')
  14. ax.w_xaxis.set_ticklabels([])
  15. ax.w_yaxis.set_ticklabels([])
  16. ax.w_zaxis.set_ticklabels([])
  17. plt.show()

实验2 鸢尾花数据可视化 - 图5
image.png