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)
AI 代码解读

目录
相关文章
如何将代码写的更加优雅?
如何将代码写的更加优雅?
85 0
如何优雅地统计代码(一)
*精美排版详见钉钉文档其实这个事情要从一个下午讲起,对我来说是个尤里卡时刻;其实一开始让我直接从数据里统计大家提交代码是有点无从下手的,前几天开始调研了一波代码统计方案后发现大部分都是基于文件来统计代码的各种行数并没有这种基于前后版本的变更代码统计,大家更多的使用Git自带的统计方法但显然我这里没有这样的环境(下面背景会详细展开),快要放弃今天的技术调研遂下楼散步刷新思维,我又回溯了我在这个项目中
x11获得窗口名的代码
x11获得窗口名的代码
144 0
代码为什么越写越乱?
这个问题往大的说是业务治理问题,往小了说是代码分拆。且看作者怎么写出好代码。
166 0
不要傻乎乎的去找不同了,一起来用代码完成“找不同”游戏吧
不要傻乎乎的去找不同了,一起来用代码完成“找不同”游戏吧
675 0
不要傻乎乎的去找不同了,一起来用代码完成“找不同”游戏吧
这一团糟的代码,真的是我写的?!
阿里妹导读:你有没有遇到过这种情况:过几周或者几个月之后,再看到自己写的代码,感觉一团糟,不禁怀疑人生?我们每天都与代码打交道,但当被问道什么是好的代码时,很多人可能会先愣一下,然后给出的回答要么比较空泛,要么比较散,没办法简单明了地概括出来。今天,我们就来说什么是好的代码?
9431 0