用Python进行数据分析的好处是,它的数据分析库目前已经很全面了,有NumPy、pandas、SciPy、scikit-learn、StatsModels,还有深度学习、神经网络的各类包。基本上能满足大部分的企业应用。

而且Python是数据抽取、数据收集整理、数据分析挖掘、数据展示,都可以在同一种Python里实现,避免了开发程序的切换。

这里就和大家分享我做的一个应用实例。解决问题:自动进行销售预测,提高准确率,减少人工一个一个SKU进行预测分析。最终的效果如下图:

1

用到的工具

当然我们只需要用Python和一些库就可以了。

  • pandas:数据整理

  • numpy:pandas的前提,科学计算

  • MySQLdb:mysql数据库链接

  • statsmodels:统计建模

  • pylab:图形处理

  • flask:web框架

2

Flask的安装

在Flask的app目录下建立一个forecasting.py的python文件,在Flask的app的templates目录下建立一个forecastin.html的模版文件,两个文件的内容如下:

forecasting.py

# -*- coding: utf-8 -*-
from app import app
from flask import render_template@app.route('/forecasting/')
def forecasting(item=None):return render_template("forecasting.html")

forecastin.html

<!doctype html>
<title>Hello World</title>
Hello World

在DOS窗口运行

python d:pyflaskrun.py

在浏览器打开http://127.0.0.1:5000/就可以看到forecasting.html模版的内容显示了。

接下来我们从头建一个预测模型。

3

建立数据库并填写数据

CREATE TABLE [Math Processing Error] ([Math Processing Error] datetime DEFAULT NULL,[Math Processing Error] float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

数据自己录入啦。

4

相关库的引入

我们现在在之前第2点建立的文件的基础上进行修改,

在forecasting.py的头部引入以下库

# -*- coding: utf-8 -*-
from app import app
from flask import render_template
import pylab
import pandas as pd
import numpy as np
from pandas import Series,DataFrame
import MySQLdb
import pandas.io.sql as sql
import statsmodels.api as sm
import time
import datetime
from dateutil.relativedelta import relativedelta
import random

5

定义路由

@app.route('/forecasting/<int:lag>')

意思就是我们访问例如http://127.0.0.1:5000/forecasting/2的地址对于就是解析到forecasting.py文件,其中<int:lag>是可变的URL部分,如上面的URL的2

6

定义函数

def forecasting(lag=None):

其中lag就是接受URL中的参数,我们定义lag是自回归函数的滞后期数。

7

数据库连接

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='bi',charset='utf8')
str_sql = "select SaleMonth as Month,Sale from sale order by SaleMonth"
sale=sql.read_sql(str_sql,conn)

8

数据处理

我们整理数据以适合使用。

##//数据处理#转换数据中的月份为日期类型,并把它定义为pandas索引sale.Month = pd.to_datetime(sale.Month)sale = sale.set_index("Month")##//提取最大月份和最小月份start = min(sale.index)end = max(sale.index)##定义预测的月份,在最大月份的基础上加1-4pre_start =end+relativedelta(months=1)pre_end =end+relativedelta(months=4)#必要的转换pre_start =pre_start.strftime('%Y-%m-%d')pre_end =pre_end.strftime('%Y-%m-%d')#生成时间序列,从最小月份到最大月份i = pd.date_range(start, end, freq='MS')df = DataFrame(i,i)#定义列、定义索引index名df.columns = ['T']df.index.names =['Month']#把sale与df合并,通过索引rs = pd.merge(sale,df,left_index=True,right_index=True,how='outer')#删除临时列T,并把rs转换为html,方便后面输出到模版中del rs['T']data = rs.to_html()

9

数据预测

##预测#对rs进行对数变换rs = np.log(rs)#对rs进行自回归,lag是自回归的滞后因子,来自函数的lag参数,即来自RUL的参数r = sm.tsa.AR(rs).fit(maxlag=lag, method='mle', disp=-1)#对未来四个月进行预测fcst_lg = r.predict(start,pre_end)#对预测的结果进行指数变换,因为之前做了对数变换fcst = np.exp(fcst_lg)#转换fcst为pandas的DataFrame格式fcst = DataFrame(fcst)#定义列名和索引,用于和原来的rs合并fcst.columns=['fcst']fcst.index.names =['Month']#合并fcst和rs到rs_outrs_out = pd.merge(sale,fcst,left_index = True,right_index = True,how='outer')#rs_out转换为记录格式,再转换为html格式,以方便输出到模版中显示#取得最后的4行作为预测的显示输出,不知道为什么rs_out[-4:-1]这个输出漏了最后一行rs_fcst = rs_out[-4:-1]rs_fcst = rs_fcst.to_html()  rs2 = rs_out.to_records()rs_out = rs_out.to_html()

10

数据整理

我使用了echart web图标框架进行显示。

##以下是处理表格数据输出到echart的json格式tmp=u"<chart caption='销售及预测拟合' subcaption='过去所有数据' xaxisname='月份' yaxisname='销售/预测' theme='fint'>"tmp1=""tmp2=""tmp3=""for t in rs2:#tmp1 += "{'label':'" + str(t.Month.year)+"/"+str(t.Month.month) + "','value':'" + str(t.Qty) + "'},"#tmp1 += "<category label='"+str(t.Month.year)+"/"+str(t.Month.month)+"' />"tmp1 += "'"+str(t.Month.year)+"/"+str(t.Month.month)+"',"#tmp2 += "<set value='"+ str('%.2f' % t.Sale) +"' />"tmp2 +=  str('%.0f' % t.Sale) +","#tmp3 += "<set value='"+ str('%.2f' % t.fcst) +"' />"tmp3 += str('%.0f' % t.fcst) +","tmp +="<categories>"+tmp1+"</categories>"tmp +=u"<dataset seriesname='销售'>"+tmp2+"</dataset>"tmp +=u"<dataset seriesname='预测' renderas='line' showvalues='0'>"+tmp3+"</dataset>"+"</chart>"tmp1 = tmp1[:-1]tmp2 = tmp2[:-1]tmp2 = tmp2.replace('nan',''-'')tmp3 = tmp3[:-1]tmp=u'''{title : {text: '测试',subtext: '纯属虚构'},tooltip : {trigger: 'axis'},legend: {data:['实际销售','预测销售']},toolbox: {show : true,feature : {mark : {show: false},dataView : {show: true, readOnly: false},magicType : {show: true, type: ['line', 'bar']},restore : {show: true},saveAsImage : {show: false}}},calculable : true,dataZoom : {show : true,realtime : true,start : 0,end : 100},xAxis : [{type : 'category',data : [%s]}],yAxis : [{type : 'value',min : 5000,scale : true}],series : [{name:'实际销售',type:'bar',data:[%s],markPoint : {data : [{type : 'max', name: '最大值'},{type : 'min', name: '最小值'}]},markLine : {data : [{type : 'average', name: '平均值'}]}},{name:'预测销售',type:'line',data:[%s],}]};''' %(tmp1,tmp2,tmp3)

11

生成公式

生成一个公式能更直观显示变量之间的关系。

#生成动态公式图片rp = r.paramsftext=''i=0for rp1 in rp:if (i==0) and (rp1>0) :const = '+' + str(("%.4f" % rp1))if (i==0) and (rp1<0) :const = str(("%.4f" % rp1))if (i==1):ftext = ftext + str(("%.4f" % rp1))+'y_{t-'+str(i)+'}'if (i>1) and (rp1>0):ftext = ftext + '+' + str(("%.4f" % rp1))+'y_{t-'+str(i)+'}'if (i>1) and (rp1<0):ftext = ftext + str(("%.4f" % rp1))+'y_{t-'+str(i)+'}'i +=1f = r'$y_{t}='+ftext+const + '$'f2 = r'$y=ln(w_{t})$'fig = pylab.figure()#设置背景为透明fig.patch.set_alpha(0)text = fig.text(0, 0, f)# 把公式用公式图片的方式保存dpi = 300fig.savefig('d:/py/formula.png', dpi=dpi)# Now we can work with text's bounding box.bbox = text.get_window_extent()width, height = bbox.size / float(dpi/4) + 0.005# Adjust the figure size so it can hold the entire text.fig.set_size_inches((width, height))# Adjust text's vertical position.dy = (bbox.ymin/float(dpi))/heighttext.set_position((0, -dy))# Save the adjusted text.url = 'D:/py/Flask/app/static/images/1.png'fig.savefig(url, dpi=dpi)

12

输出到模板

把py程序中的在模版中用到的结果输出到模版。

#生成动态公式图片rp = r.paramsftext=''i=0for rp1 in rp:if (i==0) and (rp1>0) :const = '+' + str(("%.4f" % rp1))if (i==0) and (rp1<0) :const = str(("%.4f" % rp1))if (i==1):ftext = ftext + str(("%.4f" % rp1))+'y_{t-'+str(i)+'}'if (i>1) and (rp1>0):ftext = ftext + '+' + str(("%.4f" % rp1))+'y_{t-'+str(i)+'}'if (i>1) and (rp1<0):ftext = ftext + str(("%.4f" % rp1))+'y_{t-'+str(i)+'}'i +=1f = r'$y_{t}='+ftext+const + '$'f2 = r'$y=ln(w_{t})$'fig = pylab.figure()#设置背景为透明fig.patch.set_alpha(0)text = fig.text(0, 0, f)# 把公式用公式图片的方式保存dpi = 300fig.savefig('d:/py/formula.png', dpi=dpi)# Now we can work with text's bounding box.bbox = text.get_window_extent()width, height = bbox.size / float(dpi/4) + 0.005# Adjust the figure size so it can hold the entire text.fig.set_size_inches((width, height))# Adjust text's vertical position.dy = (bbox.ymin/float(dpi))/heighttext.set_position((0, -dy))# Save the adjusted text.url = 'D:/py/Flask/app/static/images/1.png'fig.savefig(url, dpi=dpi)

13

设计模板

我们可以用{{变量名}}来接受来自py程序的变量。

<!doctype html>
<title>分析结果</title>
<script type="text/javascript" src="{{url_for('static', filename='ECharts/doc/asset/js/esl/esl.js')}}"></script><script type="text/javascript">// 路径配置require.config({paths:{ 'echarts' : '/static/ECharts/build/echarts','echarts/chart/bar' : '/static/ECharts/build/echarts','echarts/theme/macarons':'/static/ECharts/src/theme/macarons',  }});require(['echarts','echarts/theme/macarons',  'echarts/chart/bar', // 使用柱状图就加载bar模块,按需加载'echarts/chart/line' // 使用柱状图就加载bar模块,按需加载],function (ec,theme) {// 基于准备好的dom,初始化echarts图表var myChart = ec.init(document.getElementById('main'),theme); var option = {{tmp | safe}}myChart.setOption(option); });
</script>
<style>
.right{text-align: right}
body{font-size: 12px;background:white}
</style>
<div style="width:970px;"><div id="main" style="float:left;height:300px;width:600px;"></div><div style="float:left;height:300px;width:350px;margin-left:10px;">Summary of AR Results<table border=0 style="width:200px"><tr><td colspan=2></td></tr><tr><td>Lag length:</td><td class='right'>{{r.k_ar}}</td></tr><tr><td>Samples:</td><td class='right'>{{r.nobs}}</td></tr><tr><td>Model:</td><td class='right'>AR</td></tr></table>-----------------------------------------<br><table border=0 style="width:350px"><tr><td>AIC:</td><td class='right'>{{'%.4f' % r.aic}}</td><td>BIC:</td><td class='right'>{{'%.4f' % r.bic}}</td></tr><tr><td>FPE:</td><td class='right'>{{'%.4f' % r.fpe}}</td><td>HQIC:</td><td class='right'>{{'%.4f' % r.hqic}}</td></tr></table>----------------------------------------------------------<br>Results for equation<br>==========================================================<br><table border=0 style="width:280px"><tr style="border-bottom:1px solid red"><td>X</td><td class='right'>coefficient</td><td class='right'>std.error</td><td class='right'>t-stat</td><td class='right'>p-value</td></tr>{% for i in range(lag+1) %}<tr>{% if i==0 %}<td>const</td>{% else %}<td>Y(t-{{i}})</td>{% endif %}<td class='right'>{{'%.4f' % r.params[i]}}</td><td class='right'>{{'%.4f' % r.bse[i]}}</td><td class='right'>{{'%.4f' % r.tvalues[i]}}</td><td class='right'>{{'%.4f' % r.pvalues[i]}}</td></tr>{% endfor %}</table>----------------------------------------------------------<br>预测<br>==========================================================<br>  {{rs_fcst | safe}}</div>
</div>
<div style="width:970px;margin-bottom:10px;">
<img height=24 src = "../../../static/images/1.png?"><br>

14

实际应用

在这个例子中,我们只是对一个产品、一个模型、一个参数进行了预测。

在实际应用中,可以批量对产品、多个模型、多种参数进行预测,写一个判定预测模型好坏的算法,自动确定每种产品的最优模型和参数,定期自动计算各产品的预测值。

希望这个思路能帮到大家。

本文为转载分享&推荐阅读,若侵权请联系后台删除

-----------------

长按识别下方二维码,并关注公众号

1.回复“PY”领取1GB Python数据分析资料

2.回复“BG”领取3GB 名企数据分析报告

Python:从0到1销售预测建模相关推荐

  1. 为什么Python 4.0不会像Python 3.0

    在提出向后不兼容的更改(这些更改不提供从当前合法的Python 3代码提供清晰的移植路径)时,使用python-ideas的新手偶尔会参考" Python 4000"的思想. 毕竟 ...

  2. 为什么 Python 4.0 会与 Python 3.0 不同?

    [CSDN编者按]不管我们如何希望PHP永远天下第一,亦或是Java永久无敌,更或者希望C语言永远是最好的语言. 然而,笔者今天搜索百度指数得知,Python的指数,已经高于Java和PHP的指数之和 ...

  3. 虚拟魔方——使用python对普通三阶魔方进行建模

    使用python对普通三阶魔方进行建模 1 整体构想 1.1 建立虚拟魔方 1.1.1 为魔方的26个方块进行定义 1.1.2 利用图形化方式表示魔方 1.2 对魔方进行操作 1.3 实现对魔方公式的 ...

  4. 在Python里应用Openscad实现3D建模(修改简化版)之3D螺纹建模初探2

    在Python里应用Openscad实现3D建模(修改简化版)-3 –SolidPython学习笔记3 –form https://github.com/SolidCode/SolidPython 参 ...

  5. python金融应用的好书推荐卡_【荐书】智能风控:Python金融风险管理与评分卡建模(梅子行 毛鑫宇 著)...

    原标题:[荐书]智能风控:Python金融风险管理与评分卡建模(梅子行 毛鑫宇 著) 图书简介 风险管理是金融的核心,信贷场景下的风险,很大程度上取决于贷款人的信用风险.因此,如何对贷款用户的信用风险 ...

  6. 在Python里应用Openscad实现3D建模(修改简化版)之3D螺纹建模初探1

    在Python里应用Openscad实现3D建模(修改简化版)-2 –SolidPython学习笔记2 –form https://github.com/SolidCode/SolidPython 参 ...

  7. 本周AI热点回顾:Python 4.0可能不会来了;“最强Linux桌面版”竟然是Windows?

    ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍点击左上方蓝字关注我们 01 Transformer全靠数据堆?那没有数据怎么办?LUT告诉你「冇问题」|AAAI 2021 你是否曾遇到这样的场景:在陌⽣的国家旅 ...

  8. 为什么 Python 4.0 不会像 3.0 一样?

    点击上方"程序IT圈",选择"置顶公众号" 每天早晨8点50分,准点开车打卡 本文为译文,来源开源中国 https://opensource.com/life/ ...

  9. Python数据分析之共享单车及建模探索(CLV建模、可视化)

    Python数据分析之共享单车及建模探索(CLV建模.可视化) 开发环境 4.3[开发平台及环境] Windons 10 教育版 Python 3.7 IntelliJ IDEA 2018.2.1 / ...

最新文章

  1. 芯片业遭双重打击:经济低迷技术难突破
  2. 【LOJ】#2066. 「SDOI2016」墙上的句子
  3. php 代码修改后 重新实例化_从匿名函数到PHP设计模式之容器模式
  4. 动态连接库的两种方式
  5. openMVG跑自定义数据出错
  6. 在线生成文本图片 CFC函数计算版
  7. 计算机二级考试c语言公共基础知识,全国计算机二级c语言公共基础知识考试内容.doc...
  8. 循环队列CircleQueue的使用
  9. Java设计模式之单例模式(七种写法)
  10. 三次样条插值matlab,Matlab关于三次样条插值
  11. Xcode打包踩过的那些坑
  12. 程序设计方法和程序分析
  13. 高等数学(第七版)同济大学 习题1-5 个人解答
  14. Debian6.02 终端中文设置--FBTerm + ucimf
  15. kali安装Netspeed
  16. GYM MaratonIME plays Chess 模拟
  17. request_threaded_irq与request_irq
  18. 将tomcat添加和删除到服务
  19. 跟我一起做微信开发(一)——开通微信公共号(开发模式)
  20. 2006-10-01 十一皖南单车行

热门文章

  1. Python十分钟制作属于你自己的个性logo
  2. 国家大力推广【青少年人工智能教育】, 您的孩子还只是在刷题么?
  3. 【芝麻背调百科】神操作:公司给员工发解除通知,法院认定员工辞职
  4. Unity3D 绘制互动琴弦
  5. 开课吧mysql课件百度云_开课吧web全栈十二期|百度云|天翼云下载
  6. 算法性能分析(6):代码的内存消耗
  7. 抓取马上跳转的页面POST信息或者页面内容
  8. 用Linux的crontab命令来调度脚本作业
  9. 算法2_非对称加密算法之ECDSA(椭圆曲线数字签名算法)
  10. 基于51单片机的手动数字时钟