83行代码
使用SARIMAX进行时间序列预测。
#!/usr/bin/env python #-*-coding:utf-8-*- #****************************************************************************** #****************Description:Time Series prediction using SARIMAX #****************Author:Duan Tingyin #****************Date:2018.02.14 #************************************************** import pandas as pd import matplotlib.pyplot as plt import datetime from statsmodels.tsa.api import SARIMAX datapath = '../data/' train_df = pd.read_csv(datapath+'[new] yancheng_train_20171226.csv') testA_df = pd.read_csv(datapath+'yancheng_testA_20171225.csv') testB_df = pd.read_csv(datapath+'yancheng_testB_20180224.csv') train_class = train_df.groupby(['sale_date','class_id'])['sale_quantity'].sum().to_frame().reset_index() train_class.head() def plt_class(data,x,y,class_id): this_class_id=data[data.class_id == class_id] plt.scatter(x=this_class_id[x],y=this_class_id[y]) def trans_date(x): str_x=str(x) year=int(str_x[:4]) month=int(str_x[4:]) return datetime.date(year,month,1) train_class['_sale_date']=train_class['sale_date'].apply(trans_date) testA_df['_sale_date']=testA_df['predict_date'].apply(trans_date) testB_df['_sale_date']=testB_df['predict_date'].apply(trans_date) #print(train_class.head(),testA_df.head(),testB_df.head()) s="predict_date,class_id,predict_quantity" ex=[] f=open("../data/yancheng_testA_20171225.csv","r") for line in f.readlines(): if "date" in line: continue class_id=int(line.split(",")[1]) this_class_id=train_class[train_class['class_id']==class_id][['_sale_date','sale_quantity']] if class_id==653436: print(this_class_id._sale_date) #indexed_this_class_id = this_class_id.set_index(this_class_id['_sale_date']) indexed_this_class_id=this_class_id.set_index(pd.date_range(end='2017-11',periods=len(this_class_id['_sale_date']),freq='M')) print(this_class_id['_sale_date'],pd.date_range(end='2017-11',periods=len(this_class_id['_sale_date']),freq='M')) res=0 try: fit1=SARIMAX(indexed_this_class_id.sale_quantity,verbose=False).fit() pre=fit1.get_forecast().conf_int() res=(int(round((pre['lower sale_quantity'] + pre['upper sale_quantity'])*0.5))) except Exception as e: print(e) ex.append(class_id) plt_class(train_class,'sale_date','sale_quantity',class_id) res=int(this_class_id['sale_quantity'].iloc[-1]) this_class_id.to_csv('EXCEPTION'+str(class_id) +".csv",header=True,index=False,float_format='%.0f') s+="\n" s+="201711"+ ","+str(class_id) + "," +str(res) f.close() s+="\n" train_class[['sale_date','class_id','sale_quantity']].to_csv('train_class.csv',header=True,index=False,float_format='%.0f') fw=open("201711.csv","w") fw.write(s) fw.close() print(ex)
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Apache Cordova开发环境搭建(一)-Visual Studio
原文: Apache Cordova开发环境搭建(一)-Visual Studio 一、使用Visual Studio开发Apache Cordova手机App 1.版本要求,Visual Studio需求2015或2017 2.在安装的之后勾选 JavaScript应用之Apache Cordova 以及所依赖的工具就可以了。 3. Visual Studio 2015 的安装程序包括支持 Cordova 应用开发的第三方开源软件: Joyent Node.js 让 Visual Studio 能与 Apache Cordova 命令行界面 (CLI) 和 Apache Ripple 仿真器集成 为特定的 Cordova 插件手动添加 Git URI 时需要使用的 Git CLI。 使用 Google Chrome在 iOS 和 Android 上运行 Apache Ripple 仿真器 Android 生成过程需要用到的 Apache Ant 1.8.0 或更高版本。 Android 生成过程需要用到 32 位的 Oracle Java JDK 7。注意: 不支持 64 位的 Ja...
- 下一篇
编译型语言、解释型语言、静态类型语言、动态类型语言概念与区别
今天看到篇对于静态、动态、强类型、弱类型总结的很到位的一篇博文,在此Mark下。 编译型语言和解释型语言 1、编译型语言 需通过编译器(compiler)将源代码编译成机器码,之后才能执行的语言。一般需经过编译(compile)、链接(linker)这两个步骤。编译是把源代码编译成机器码,链接是把各个模块的机器码和依赖库串连起来生成可执行文件。 优点:编译器一般会有预编译的过程对代码进行优化。因为编译只做一次,运行时不需要编译,所以编译型语言的程序执行效率高。可以脱离语言环境独立运行。 缺点:编译之后如果需要修改就需要整个模块重新编译。编译的时候根据对应的运行环境生成机器码,不同的操作系统之间移植就会有问题,需要根据运行的操作系统环境编译不同的可执行文件。 代表语言:C、C++、Pascal、Object-C以及最近很火的苹果新语言swift 2、解释型语言 解释性语言的程序不需要编译,相比编译型语言省了道工序,解释性语言在运行程序的时候才逐行翻译。 优点:有良好的平台兼容性,在任何环境中都可以运行,前提是安装了解释器(虚拟机)。灵活,修改代码的时候直接修改就可以,可以快速部署,不用停...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器