python贡献度分析以及相关性分析小例子

来源:互联网 发布:web软件测试工具 编辑:程序博客网 时间:2024/05/22 00:32

贡献度又称帕累托,80%的利润来自20%的最畅销的产品,而其他80%的产品只产生了20%的利润。

#-*- coding: utf-8 -*-#菜品盈利数据 帕累托图from __future__ import print_functionimport pandas as pd#初始化参数dish_profit = 'E:/PythonMaterial/chapter3/chapter3/demo/data/catering_dish_profit.xls' #餐饮菜品盈利数据data = pd.read_excel(dish_profit, index_col = u'菜品名')data = data[u'盈利'].copy()#把data的盈利这一列抓出来data.sort(ascending = False)#降序排列import matplotlib.pyplot as plt #导入图像库plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号plt.figure()#建立图像#plot函数用来绘制线性二维图,折线图;kind用来指定作图类型,bar指的是条形图data.plot(kind='bar')plt.ylabel(u'盈利(元)')#sum()是计算样本的总和;cumsum()是依次给出前1,2,3.。。n个数的和p = 1.0*data.cumsum()/data.sum()p.plot(color = 'r', secondary_y = True, style = '-o',linewidth = 2)plt.annotate(format(p[6], '.4%'), xy = (6, p[6]), xytext=(6*0.9, p[6]*0.9), arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) #添加注释,即85%处的标记。这里包括了指定箭头样式。plt.ylabel(u'盈利(比例)')plt.show()

这里写图片描述

#-*- coding: utf-8 -*-#餐饮销量数据相关性分析import pandas as pdcatering_sale = 'E:/PythonMaterial/chapter3/chapter3/demo/data/catering_sale_all.xls' #餐饮数据,含有其他属性data = pd.read_excel(catering_sale, index_col = u'日期') #读取数据,指定“日期”列为索引列d1=data.corr() #相关系数矩阵,即给出了任意两款菜式之间的相关系数print d1print "\n"d2=data.corr()[u'百合酱蒸凤爪'] #只显示“百合酱蒸凤爪”与其他菜式的相关系数print d2print '\n'*3#打印换行单引号双引号都行,*3表示打印3个空行d3=data[u'百合酱蒸凤爪'].corr(data[u'翡翠蒸香茜饺']) #计算“百合酱蒸凤爪”与“翡翠蒸香茜饺”的相关系数print d3
           百合酱蒸凤爪    翡翠蒸香茜饺   金银蒜汁蒸排骨     乐膳真味鸡     蜜汁焗餐包      生炒菜心    铁板酸菜豆腐  \百合酱蒸凤爪   1.000000  0.009206  0.016799  0.455638  0.098085  0.308496  0.204898   翡翠蒸香茜饺   0.009206  1.000000  0.304434 -0.012279  0.058745 -0.180446 -0.026908   金银蒜汁蒸排骨  0.016799  0.304434  1.000000  0.035135  0.096218 -0.184290  0.187272   乐膳真味鸡    0.455638 -0.012279  0.035135  1.000000  0.016006  0.325462  0.297692   蜜汁焗餐包    0.098085  0.058745  0.096218  0.016006  1.000000  0.308454  0.502025   生炒菜心     0.308496 -0.180446 -0.184290  0.325462  0.308454  1.000000  0.369787   铁板酸菜豆腐   0.204898 -0.026908  0.187272  0.297692  0.502025  0.369787  1.000000   香煎韭菜饺    0.127448  0.062344  0.121543 -0.068866  0.155428  0.038233  0.095543   香煎罗卜糕   -0.090276  0.270276  0.077808 -0.030222  0.171005  0.049898  0.157958   原汁原味菜心   0.428316  0.020462  0.029074  0.421878  0.527844  0.122988  0.567332               香煎韭菜饺     香煎罗卜糕    原汁原味菜心  百合酱蒸凤爪   0.127448 -0.090276  0.428316  翡翠蒸香茜饺   0.062344  0.270276  0.020462  金银蒜汁蒸排骨  0.121543  0.077808  0.029074  乐膳真味鸡   -0.068866 -0.030222  0.421878  蜜汁焗餐包    0.155428  0.171005  0.527844  生炒菜心     0.038233  0.049898  0.122988  铁板酸菜豆腐   0.095543  0.157958  0.567332  香煎韭菜饺    1.000000  0.178336  0.049689  香煎罗卜糕    0.178336  1.000000  0.088980  原汁原味菜心   0.049689  0.088980  1.000000  百合酱蒸凤爪     1.000000翡翠蒸香茜饺     0.009206金银蒜汁蒸排骨    0.016799乐膳真味鸡      0.455638蜜汁焗餐包      0.098085生炒菜心       0.308496铁板酸菜豆腐     0.204898香煎韭菜饺      0.127448香煎罗卜糕     -0.090276原汁原味菜心     0.428316Name: 百合酱蒸凤爪, dtype: float640.00920580305184
0 0