博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
83行代码
阅读量:7125 次
发布时间:2019-06-28

本文共 2731 字,大约阅读时间需要 9 分钟。

使用SARIMAX进行时间序列预测。

#!/usr/bin/env python#-*-coding:utf-8-*-#******************************************************************************#****************Description:Time Series prediction using SARIMAX#****************Author:Duan Tingyin#****************Date:2018.02.14#**************************************************import pandas as pdimport matplotlib.pyplot as pltimport datetimefrom statsmodels.tsa.api  import SARIMAXdatapath = '../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)

转载地址:http://wheel.baihongyu.com/

你可能感兴趣的文章
《OSPF和IS-IS详解》一第2章 理解BGP的构件块
查看>>
2016 年 Android 市场的 8 个大胆预测
查看>>
timeago.js 1.1.0,一个极简的 Javascript 库
查看>>
高考将至,听听那些关于高考的经典段子吧
查看>>
如何在树莓派上使用图片特效
查看>>
《PHP和MySQL Web开发从新手到高手(第5版)》一一2.7 查看存储的数据
查看>>
在 Linux 下使用 RAID(六):设置 RAID 10 或 1 + 0(嵌套)
查看>>
《Adobe Fireworks CS5中文版经典教程》——1.6 撤销操作
查看>>
《Cucumber:行为驱动开发指南》——2.2 创建一个特性
查看>>
企业IT架构转型之道:阿里巴巴中台战略思想与架构实战. 1.2 企业信息中心发展的症结...
查看>>
《精通Python网络爬虫:核心技术、框架与项目实战》——第一篇 Part 1 理论基础篇 第1章 什么是网络爬虫 1.1 初识网络爬虫...
查看>>
蚂蚁金服寒泉子:JVM源码分析之临门一脚的OutOfMemoryError完全解读
查看>>
插入排序
查看>>
跨域iframe高度自适应 改进版(兼容)
查看>>
[Python爬虫] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题...
查看>>
codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
查看>>
《深入理解并行编程》中文版
查看>>
lintcode Permutation Index
查看>>
线程管理(八)在线程里处理不受控制的异常
查看>>
Cookie问题(烦了三天)
查看>>