文章目录
- 1、定义
- 2、 定性分析
- 2.1 柱状图
- 2.2 饼图
- 3、定量分析
- 3.1 直方图
- 3.2 累计曲线图
- 4、关系分析(散点图)
- 5、探索分析(箱线图)
- 6、回顾
- 7、源码
- 8、参考资料
1、定义
首先介绍两个概念
- 频数:同一变量值出现的次数
- 频率:频数占所有出现的次数总和的比例
为了进行频数分析,我们采用一组容量为10000的男学生身高,体重,成绩作为数据集进行定性
和定量
的分析,将此模型运用于类似的问题。
创建数据集
def genData():
heights = []
weights = []
grades = []
N = 10000
for i in range(N):
while True:
#身高服从均值172,标准差为6的正态分布
height = normal(172, 6)
if 0 < height: break
while True:
#体重由身高作为自变量的线性回归模型产生,误差服从标准正态分布
weight = (height - 80) * 0.7 + normal(0, 1)
if 0 < weight: break
while True:
#分数服从均值为70,标准差为15的正态分布
score = normal(70, 15)
if 0 <= score and score <= 100:
grade = 'E' if score < 60 else ('D' if score < 70 else ('C' if score < 80 else ('B' if score < 90 else 'A')))
break
heights.append(height)
weights.append(weight)
grades.append(grade)
return array(heights), array(weights), array(grades)
heights, weights, grades = genData()
2、 定性分析
2.1 柱状图
柱状图是以柱的高度来指代某种类型的频数,使用Matplotlib对成绩这一定性变量绘制柱状图的代码如下:
#绘制柱状图
def drawBar(grades):
t = Counter(grades)
xticks = ['A', 'B', 'C', 'D', 'E']
gradeGroup = {}
for i in range(len(xticks)):
gradeGroup[xticks[i]] = t.get(xticks[i])
print(gradeGroup)
#创建柱状图
#第一个参数为柱的横坐标
#第二个参数为柱的高度
#参数align为柱的对齐方式,以第一个参数为参考标准
pyplot.bar(range(5), [gradeGroup.get(xtick) for xtick in xticks], align='center')
#设置柱的文字说明
#第一个参数为文字说明的横坐标
#第二个参数为文字说明的内容
pyplot.xticks(range(5), xticks)
pyplot.xlabel('Grade')
pyplot.ylabel('Frequency')
pyplot.title('Grades Of Male Students')
pyplot.show()
drawBar(grades)
2.2 饼图
而饼形图是以扇形的面积来指代某种类型的频率,使用Matplotlib对成绩这一定性变量绘制饼形图的代码如下:
#绘制饼形图
def drawPie(grades):
labels = ['A', 'B', 'C', 'D', 'E']
t = Counter(grades)
xticks = ['A', 'B', 'C', 'D', 'E']
gradeGroup = {}
for i in range(len(xticks)):
gradeGroup[xticks[i]] = t.get(xticks[i])
#创建饼形图
#第一个参数为扇形的面积
#labels参数为扇形的说明文字
#autopct参数为扇形占比的显示格式
pyplot.pie([gradeGroup.get(label) for label in labels], labels=labels, autopct='%1.1f%%')
pyplot.title('Grades Of Male Students')
pyplot.show()
drawPie(grades)
3、定量分析
3.1 直方图
直方图类似于柱状图,是用柱的高度来指代频数,不同的是其将定量数据划分为若干连续的区间,在这些连续的区间上绘制柱。使用Matplotlib对身高这一定量变量绘制直方图的代码如下:
#绘制直方图
def drawHist(heights):
#创建直方图
#第一个参数为待绘制的定量数据,不同于定性数据,这里并没有事先进行频数统计
#第二个参数为划分的区间个数
pyplot.hist(heights, 100)
pyplot.xlabel('Heights')
pyplot.ylabel('Frequency')
pyplot.title('Heights Of Male Students')
pyplot.show()
drawHist(heights)
3.2 累计曲线图
累积曲线对应数据的分布函数,由于身高变量是属于服从正态分布的,从绘制出来的累积曲线图上也可以直观地看出来:
#绘制累积曲线
def drawCumulativeHist(heights):
#创建累积曲线
#第一个参数为待绘制的定量数据
#第二个参数为划分的区间个数
#normed参数为是否无量纲化
#histtype参数为'step',绘制阶梯状的曲线
#cumulative参数为是否累积
pyplot.hist(heights, 20, density=True, histtype='step', cumulative=True)
pyplot.xlabel('Heights')
pyplot.ylabel('Frequency')
pyplot.title('Heights Of Male Students')
pyplot.show()
drawCumulativeHist(heights)
4、关系分析(散点图)
在散点图中,分别以自变量和因变量作为横纵坐标。当自变量与因变量线性相关时,在散点图中,点近似分布在一条直线上。我们以身高作为自变量,体重作为因变量,讨论身高对体重的影响。使用Matplotlib绘制散点图的代码如下:
#绘制散点图
def drawScatter(heights, weights):
#创建散点图
#第一个参数为点的横坐标
#第二个参数为点的纵坐标
pyplot.scatter(heights, weights)
pyplot.xlabel('Heights')
pyplot.ylabel('Weights')
pyplot.title('Heights & Weights Of Male Students')
pyplot.show()
drawScatter(heights, weights)
5、探索分析(箱线图)
在不明确数据分析的目标时,我们对数据进行一些探索性的分析,通过我们可以知道数据的中心位置,发散程度以及偏差程度。使用Matplotlib绘制关于身高的箱形图的代码如下:
#绘制箱形图
def drawBox(heights):
#创建箱形图
#第一个参数为待绘制的定量数据
#第二个参数为数据的文字说明
pyplot.boxplot([heights], labels=['Heights'])
pyplot.title('Heights Of Male Students')
pyplot.show()
drawBox(heights)
绘制出来的箱形图中,包含3种信息:
- Q2所指的红线为中位数
- Q1所指的蓝框下侧为下四分位数,Q3所指的蓝框上侧为上四分位数,Q3-Q1为四分为差。四分位差也是衡量数据的发散程度的指标之一。
- 上界线和下界线是距离中位数1.5倍四分位差的线,高于上界线或者低于下界线的数据为异常值。
6、回顾
方法 | 说明 |
bar | 柱状图 |
pie | 饼图 |
hist | 直方图&累计曲线 |
scatter | 散点图 |
boxplot | 箱线图 |
xticks | 设置柱横坐标的文字说明 |
yticks | 设置柱纵坐标的文字说明 |
xlabel | 横坐标的文字说明 |
ylabel | 纵坐标的文字说明 |
title | 标题 |
show | 绘图 |
7、源码
import numpy as np
import random
import matplotlib.pyplot as pyplot
from numpy import array
from numpy.random import normal
from collections import Counter
def genData():
heights = []
weights = []
grades = []
N = 10000
for i in range(N):
while True:
#身高服从均值172,标准差为6的正态分布
height = normal(172, 6)
if 0 < height: break
while True:
#体重由身高作为自变量的线性回归模型产生,误差服从标准正态分布
weight = (height - 80) * 0.7 + normal(0, 1)
if 0 < weight: break
while True:
#分数服从均值为70,标准差为15的正态分布
score = normal(70, 15)
if 0 <= score and score <= 100:
grade = 'E' if score < 60 else ('D' if score < 70 else ('C' if score < 80 else ('B' if score < 90 else 'A')))
break
heights.append(height)
weights.append(weight)
grades.append(grade)
return array(heights), array(weights), array(grades)
heights, weights, grades = genData()
#绘制柱状图
def drawBar(grades):
t = Counter(grades)
xticks = ['A', 'B', 'C', 'D', 'E']
gradeGroup = {}
for i in range(len(xticks)):
gradeGroup[xticks[i]] = t.get(xticks[i])
print(gradeGroup)
#创建柱状图
#第一个参数为柱的横坐标
#第二个参数为柱的高度
#参数align为柱的对齐方式,以第一个参数为参考标准
pyplot.bar(range(5), [gradeGroup.get(xtick) for xtick in xticks], align='center')
#设置柱的文字说明
#第一个参数为文字说明的横坐标
#第二个参数为文字说明的内容
pyplot.xticks(range(5), xticks)
pyplot.xlabel('Grade')
pyplot.ylabel('Frequency')
pyplot.title('Grades Of Male Students')
pyplot.show()
drawBar(grades)
# 绘制饼形图
def drawPie(grades):
labels = ['A', 'B', 'C', 'D', 'E']
t = Counter(grades)
xticks = ['A', 'B', 'C', 'D', 'E']
gradeGroup = {}
for i in range(len(xticks)):
gradeGroup[xticks[i]] = t.get(xticks[i])
# 创建饼形图
# 第一个参数为扇形的面积
# labels参数为扇形的说明文字
# autopct参数为扇形占比的显示格式
pyplot.pie([gradeGroup.get(label) for label in labels], labels=labels, autopct='%1.1f%%')
pyplot.title('Grades Of Male Students')
pyplot.show()
drawPie(grades)
#绘制直方图
def drawHist(heights):
#创建直方图
#第一个参数为待绘制的定量数据,不同于定性数据,这里并没有事先进行频数统计
#第二个参数为划分的区间个数
pyplot.hist(heights, 100)
pyplot.xlabel('Heights')
pyplot.ylabel('Frequency')
pyplot.title('Heights Of Male Students')
pyplot.show()
drawHist(heights)
#绘制累积曲线
def drawCumulativeHist(heights):
#创建累积曲线
#第一个参数为待绘制的定量数据
#第二个参数为划分的区间个数
#normed参数为是否无量纲化
#histtype参数为'step',绘制阶梯状的曲线
#cumulative参数为是否累积
pyplot.hist(heights, 20, density=True, histtype='step', cumulative=True)
pyplot.xlabel('Heights')
pyplot.ylabel('Frequency')
pyplot.title('Heights Of Male Students')
pyplot.show()
drawCumulativeHist(heights)
#绘制散点图
def drawScatter(heights, weights):
#创建散点图
#第一个参数为点的横坐标
#第二个参数为点的纵坐标
pyplot.scatter(heights, weights)
pyplot.xlabel('Heights')
pyplot.ylabel('Weights')
pyplot.title('Heights & Weights Of Male Students')
pyplot.show()
drawScatter(heights, weights)
#绘制箱形图
def drawBox(heights):
#创建箱形图
#第一个参数为待绘制的定量数据
#第二个参数为数据的文字说明
pyplot.boxplot([heights], labels=['Heights'])
pyplot.title('Heights Of Male Students')
pyplot.show()
drawBox(heights)