83行代码

简介: #!/usr/bin/env python #-*-coding:utf-8-*- #****************************************************************************** #****************Descripti.


使用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)

目录
相关文章
|
3月前
关于代码的思考
关于代码的思考
8 0
|
3月前
流星雨代码
流星雨代码
|
4月前
|
前端开发 Java C++
一行代码就能完成的事情,为什么要写两行?
一行代码就能完成的事情,为什么要写两行?
|
5月前
你写过的最蠢的代码是?
你写过的最蠢的代码是?
16 0
|
6月前
|
设计模式 存储 Java
写出漂亮代码的45个小技巧(上)
大家好,我是三友~~ 不知道大家有没有经历过维护一个已经离职的人的代码的痛苦,一个方法写老长,还有很多的if else ,根本无法阅读,更不知道代码背后的含义,最重要的是没有人可以问,此时只能心里默默地问候这个留坑的兄弟。。
写出漂亮代码的45个小技巧(上)
|
存储 分布式计算 并行计算
聊聊什么代码是好代码
聊聊什么代码是好代码
代码流星雨
代码流星雨
79 0
代码流星雨
|
SQL 人工智能 前端开发
无代码的未来
随着无代码技术越来越成熟,很多web应用已经可以基于无代码平台进行开发。本文分析了4个最流行的无代码平台,并梳理了无代码行业今后可能的发展方向。
375 0
无代码的未来
|
测试技术 UED 开发者
被劣质代码“残害”的这些年
都已经 2020 年了,但我们仍然在生产劣质软件。自从计算机诞生以来,已经过去了近 70 年,但我们似乎还没有吸取所有的教训,仍然在犯着重复的错误。