数据的初步诊断
若数据量适中,人为观察是直观可行的方式 一般情况下,需要借助外部工具和手段进行数据的初步判定
数据可视化借助图形,图标等手段,从数据集中抽取有效信息,并对其进行展观 从视觉上快速感知、了解数据集 多维度观察“平面”数据
Matplotlib是Python最著名的绘图库 提供于Matlab相似的命令API,适合交互式制图 方便地将它作为绘图控件,嵌入到GUI应用程序中 绘图文档完备,在Gallery页面中有上百幅缩略图和源程序可供查看
采集数据 数据可视化交接数据统计特征 可绘制散点图,折线图,分布图,箱线图等 充分了解数据,再进行数据分析
import pandas as pd
import numpy as np
iris = pd.read_csv('./iris.csv')
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax1 = plt.subplot(111)
ax1.plot(iris['Sepal.Length'], 'k--', label='Sepal.Length')
# 'k--' 为折现的形状或颜色,label为图例的名称
ax1.legend(loc='best') # 设置图例的位置
ax1
ax1 = plt.subplot(111)
ax1.plot(iris['Sepal.Length'], 'b-', label='Sepal.Length')
ax1.legend(loc='best')
ax1
ax1 = plt.subplot(111)
ax1.plot(iris['Sepal.Length'], 'r*', label='Sepal.Length')
ax1.legend(loc='best')
ax1
散点图可体现两个属性值的对比
ax2 = plt.subplot(111)
ax2.scatter(iris['Sepal.Width'],iris['Petal.Width'] , label= 'petal&sepal_width')
ax2.legend(loc='best')
plt.xlabel('Sepal.Width')
plt.ylabel('Petal.Width')
上图我们可以看到是不相关的,如果相关的话是会聚集到对角线附近
如果想要查看属性的分布情况时,可以使用条形图
ax3 = plt.subplot(111)
a = ax3.hist(iris['Sepal.Width'],bins =10, label='Sepal.Width')
ax3.legend(loc='best')
drawstyle用于选择插值方式
plt.plot(iris['Petal.Length'], drawstyle = 'steps')
用于设置坐标轴范围
plt.plot(iris['Petal.Length'], drawstyle = 'steps')
plt.xlim([0, 50])
plt.plot(iris['Petal.Length'], drawstyle = 'steps')
plt.ylim([0, 50])
ax= plt.subplot(111)
# 设置坐标轴现实
ax.plot(iris['Sepal.Length'], 'g-', label = '_nolengend_')
# 设置坐标位置
ticks = ax.set_xticks([30, 60 , 90, 120])
# 设置该位置显示的文字,以及文字旋转角度,文字大小
labels = ax.set_xticklabels(['30', '60', '90', '120'], rotation = 50, fontsize = 'large')
箱纸图可用于显示数据的分布情况
ax= plt.subplot(111)
# whis指定离群点分割线
ax.boxplot(iris['Sepal.Length'], whis=2)
# plt.title()设置标题
plt.title('box plot')
# plt.ylabel()设置坐标轴名称
plt.ylabel('data content')