数据特征分析主要包括分布分析、对比分析、统计量分析、周期性分析、贡献度分析、相关性分析几种分析。
分布分析
分布分析的最终成果是形成能体现数据的图表
分布分析主要有两种类型:对定量数据的分布分析和对定性数据的分布分析
对定量数据的分布分析
要形成一个图表的话对于定量数据最有代表性的就是频率分布直方图。构造频率分布直方图的步骤初中时已经学过,顺序为求极差,设定组距分组,统计每组的频数,算出每组的频率,最后绘图.
具体的代码如下:
# 代码3-3 捞起生鱼片的季度销售情况
import pandas as pd
import numpy as np
catering_sale = 'D:\DataMiningCode\chapter3\demo\data\catering_fish_congee.xls' # 餐饮数据
data = pd.read_excel(catering_sale,names=['date','sale']) # 读取数据,指定“日期”列为索引,前面那个位置放地址,后面的names放获取的对象,这里xls表只有两列,可能是命名第一列为
#data,后一列为sale,并且指定第一列的日期为索引##如果下面这段代码不打的话频率分布直方图会粘在一起,而且横坐标会改变,默认为一段一段不会有组中值,应该是书的代码不匹配
##bins和labels这两个列表是用来服务data['sale分层']这个语句的
#bins=[0,500,1000,1500,2000,2500,3000,3500,4000]#bins 参数的含义是所画出的直方图的“柱”的个数;每个“柱”的值为其跨越的值的个数和。这里应该是横坐标分点的横坐标的值
#labels=['[0,500)','[500,1000)','[1000,1500)','[1500,2000)','[2000,2500)','[2500,3000)','[3000,3500)','[3500,4000)']#labels是标签,是每一个柱子的展示标签,写在横坐标的下面
#data['sale分层']=pd.cut(data.sale,bins, labels=labels)#建议看博客pandas的cut方法,第一个位置是我们要进行切割的列表;bins是切片的方式,可以是数字,可以是列表;lables是每个柱子的标
##这个表的名称是sale分层
##下面这两行代码建议看博客里面的方法(无语住,我实在不会了)
#aggResult=data.groupby(by=['sale分层'])['sale'].agg({'sale': np.size})
#pAggResult=round(aggResult/aggResult.sum(),2,)*100import matplotlib.pyplot as plt
d = 500 # 设置组距
num_bins = round((max(data['sale']) - min(data['sale'])) / d) # 计算组数,用最大值减最小值除以设定好的组距
plt.figure(figsize=(10,6)) # 设置图框大小尺寸
plt.hist(data['sale'], num_bins)#hist方法的第一个位置为要处理的数据集;第二个位置放bins,可以为数组可以为整数,如果是整数的话就是分成几组的意思
plt.xticks(range(0, 4000, d))#每一个组距d输出一个x轴的标签
plt.xlabel('sale分层')#x轴的标记为sale分层
plt.grid()#用grid()方法为图画上格子
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.title('季度销售额频率分布直方图',fontsize=20)#显示标题,fontsize用来设置字体的大小
plt.show()#展示图
我出现的问题:(这里的代码我用#标记了,其实这个问题不用看,原因是不同版本pandas库的书写问题)
解决办法:暂未解决,目前知道的是pandas库更新出现的语句形式的转变问题(不要看 )
https://blog.csdn.net/KIKI_ZSH/article/details/118722642?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167827984316782425146513%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167827984316782425146513&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-118722642-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=groupby%E5%87%BD%E6%95%B0&spm=1018.2226.3001.4187https://blog.csdn.net/KIKI_ZSH/article/details/118722642?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167827984316782425146513%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167827984316782425146513&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-118722642-null-null.142%5Ev73%5Einsert_down1,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&utm_term=groupby%E5%87%BD%E6%95%B0&spm=1018.2226.3001.4187
我出现的问题:
不了解pandas库里的cut方法
解决办法:
https://blog.csdn.net/heianduck/article/details/124409593?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167827918816800186528145%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167827918816800186528145&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-124409593-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=pandas%E7%9A%84cut%E5%87%BD%E6%95%B0&spm=1018.2226.3001.4187https://blog.csdn.net/heianduck/article/details/124409593?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167827918816800186528145%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167827918816800186528145&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-124409593-null-null.142%5Ev73%5Einsert_down1,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&utm_term=pandas%E7%9A%84cut%E5%87%BD%E6%95%B0&spm=1018.2226.3001.4187
我出现的问题:
忘记round函数的用法
解决办法:
https://blog.csdn.net/qq_34035425/article/details/123237006?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167827607716800192283930%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167827607716800192283930&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-123237006-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=round%E5%87%BD%E6%95%B0&spm=1018.2226.3001.4187https://blog.csdn.net/qq_34035425/article/details/123237006?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167827607716800192283930%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167827607716800192283930&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-123237006-null-null.142%5Ev73%5Einsert_down1,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&utm_term=round%E5%87%BD%E6%95%B0&spm=1018.2226.3001.4187
我出现的问题:
不了解matplotlib里面的hist做直方图的方法
解决办法:
https://blog.csdn.net/chongbaikaishi/article/details/108965600?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828084316800211596945%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828084316800211596945&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-108965600-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=hist&spm=1018.2226.3001.4187https://blog.csdn.net/chongbaikaishi/article/details/108965600?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828084316800211596945%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828084316800211596945&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-4-108965600-null-null.142%5Ev73%5Einsert_down1,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&utm_term=hist&spm=1018.2226.3001.4187
我出现的问题:
不了解matplotlib里面的xtricks做为横坐标标记注释的方法
解决办法:
https://blog.csdn.net/u011208984/article/details/90720516?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828145616800182713174%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828145616800182713174&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-90720516-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=xticks&spm=1018.2226.3001.4187https://blog.csdn.net/u011208984/article/details/90720516?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828145616800182713174%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828145616800182713174&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-90720516-null-null.142%5Ev73%5Einsert_down1,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&utm_term=xticks&spm=1018.2226.3001.4187
我出现的问题:
不了解matplotlib里面的grid()方法
解决办法:
发现它其实就是加上格子
运行结果:
对定性数据的分布分析
对定性数据,通常只能采用饼图和条形图来展示,因为只能统计一下定性数据的比例。
具体代码:
# 代码3-4 不同菜品在某段时间的销售量的分布情况
import pandas as pd
import matplotlib.pyplot as plt
catering_dish_profit = 'D:\DataMiningCode\chapter3\demo\data\catering_dish_profit.xls' # 餐饮数据
data = pd.read_excel(catering_dish_profit) # 读取数据,指定“日期”列为索引# 绘制饼图
x = data['盈利'] #用x标记data中“盈利”这列
labels = data['菜品名']#用labels标记data当中的“菜品名”这列
plt.figure(figsize = (8, 6)) # 设置画布大小
plt.pie(x,labels=labels) # 绘制饼图,第一个位置是标记待处理的数据盈利这列的盈利额,第二个位置显示每个区域对应的描述文本
plt.rcParams['font.sans-serif'] = 'SimHei'#用来正常显示中文标签
plt.title('菜品销售量分布(饼图)') # 设置标题
plt.axis('equal')#plt.axis(‘equal’)表示 x,y轴刻度等长
plt.show()# 绘制条形图
x = data['菜品名']#用x标记data中“菜品名”这列
y = data['盈利']#用y标记data中“盈利”这列
plt.figure(figsize = (8, 4)) # 设置画布大小
plt.bar(x,y)#bar方法用来绘制柱状图,其中第一第二的位置分别是x轴和y轴的数值
plt.rcParams['font.sans-serif'] = 'SimHei'#用来正常显示中文标签
plt.xlabel('菜品') # 设置x轴标题
plt.ylabel('销量') # 设置y轴标题
plt.title('菜品销售量分布(条形图)') # 设置标题
plt.show() # 展示图片
我遇到的问题:
不了解pie方法绘制饼图
解决办法:
https://blog.csdn.net/m0_37149062/article/details/120307950?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828551216800188573262%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828551216800188573262&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-120307950-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=pie%E7%BB%98%E5%88%B6%E9%A5%BC%E5%9B%BE&spm=1018.2226.3001.4187https://blog.csdn.net/m0_37149062/article/details/120307950?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828551216800188573262%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828551216800188573262&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-120307950-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=pie%E7%BB%98%E5%88%B6%E9%A5%BC%E5%9B%BE&spm=1018.2226.3001.4187
我遇到的问题:
不了解anxi方法让xy两个轴等长
解决办法:
https://blog.csdn.net/qq_36439087/article/details/121527402?ops_request_misc=&request_id=&biz_id=102&utm_term=plt.axis(%27equal%27)&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-121527402.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&spm=1018.2226.3001.4187https://blog.csdn.net/qq_36439087/article/details/121527402?ops_request_misc=&request_id=&biz_id=102&utm_term=plt.axis(%27equal%27)&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-121527402.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&spm=1018.2226.3001.4187
我遇到的问题:
不了解bar方法绘制柱状图
解决办法:
https://blog.csdn.net/weixin_46707493/article/details/119829962?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828587516800227479011%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828587516800227479011&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-119829962-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=plt.bar%28x%2Cy%29&spm=1018.2226.3001.4187https://blog.csdn.net/weixin_46707493/article/details/119829962?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167828587516800227479011%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167828587516800227479011&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-119829962-null-null.142^v73^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=plt.bar%28x%2Cy%29&spm=1018.2226.3001.4187
运行结果: