赣州新闻网

首页 / 科技

通过 Python 代码实现时间序列数据的统计学预测模型

2020-04-14 · 赣州新闻网 · 科技


通过 Python 代码实现时间序列数据的统计学预测模型


通过 Python 代码实现时间序列数据的统计学预测模型

来源 | DeepHub IMBA

封图 | CSDN 付费下载于视觉中国

在本篇中,我们将展示使用 Python 统计学模型进行时间序列数据分析。 目标是:根据两年以上的每日广告支出历史数据,提前预测两个月的广告支出金额。

原始数据:2017-01-01 到 2019-09-23 期间的每日广告支出。

通过 Python 代码实现时间序列数据的统计学预测模型

数据准备:划分训练集和测试集。

 df1 = data[ epend\. U =']].set_index(\'Date\') trainz 6 Q M K m + = df1.iloct, ? f u vest = df1.iloc test.shape,train.shape

测试集大小:(63,1);训h / 6 U T G = s练集大小:(933,1)。

通过 Python 代码实现时间序列数据的统计学预测模型

统计模型与统计要素

时间序列分析常用统计模型

  • 单变量时间序列统计学模型,如:平均方法、平滑方R 7 ? k @ E f D法、有/无季节W a G M u ~性条件的 ARIMA 模G ^ 为时间序列周期。

    案例:通过7 R V + ] SARIMA 预测广告支出

    首先,我们建立d 0 G / o e @ .test_stationarity来检查时间序列数据的平稳性。

    from statsmodels.tsa.stattools i3 R Z xmport adfuller df1=df.resample(\'D\'a E c E / 9 x j /, how=np.mean)def test_stationarity(timeseries): rolmeanH d # * 9 -  8 f = timeseries.rolling(window=30).mean rols/ a * I 2 i |td = timeserieu H ^  ds.ros { n U ; + _ p ;lling(window=30).std plt.figure(figsize=(1_ 6 .4,5)) sns.despine(left=True) orig = plt.plot(timeseries, color=\'blue\',label=\'Original\') mean = plt.plot(rolmean, color=\'red\', label=\'Rolling Mean\') st!  l U 0d = plt.plot(rolstd, color=\t d V D ~ Z R o'black\', label = \'Rolling Std\') plt.legend(loc=\'best\'); plt.title(\% 3 K h'Rolling Mean & Standard Deviation\') plt.showprint (\'<Results of Dickey-Fuller Test>\') dftest = adfuller(timeseries, autolag=\'AIC\') dfoutput = pd.SerY w [ q ries(dftest, ind1 2 9ex=)for key,value M # T ^ 0 in dftest.items: dfoutput = value print(dfoutput) test_stati& C K r 7 D #onarity(df1.Spend.dropna)

    通过 test_stationaritF L e c ~ +y函X J %数,可以绘制移动平均值以及标准差,并且通过 Augmented Dickey-FulleW | ; x ir test 输出 P 值。

    通过 Python 代码实现时间序列数据的统计学预测模型

    对比临界值(critical value)可以看到,时间序列数据时非平稳的。

    首先我们试试对数变换,能不能使数据达到平稳性要求。

    通过 Python 代码实现时间序列数据的统计学预测模型

    可以看到,利用对数变换df1=np.log(df1),时间序列在阈值为5%时满足平稳性要求。接= z * r 3下来,我们试试差分操作:test_stationarity(df1.di+ w E p + wff(1).dropna)。

    通过 Python 代码实现时间序列数据的统计学预测模型

    显然,通过差分操作后,效果更好,时间序列在阈值为1%时满足平稳性要求。

    然后,我们就可以建立 SARIMA 模型,并且预测 2019-07-23 到 2019-09-23 这两个月间每天B | Y c T T f K 4的广告j @ + F n &指出。

    import statsmodels.api as sm fit1 =F ` m y . j j sm.tsa.statespace.SARI1  = B K X KMAX(train.Spend^ M Q, order=(7, 1,& { ] 3 y 2), seasonal_order=(0, 1, 2, 7)).fit(use_boxcox=True) test = fit1k } E 6.predict(start=\"2019-07-23\", end=\V 3 }  r G D V"2019-09-23\", dynamic=True) plt.figure(fi] ~ *gsize=(16, 8)) plt.plot(trainn R =  Z l, label=\'Train\') px i 5 W _ }lt.plot(test, label=\'Test\') plt.plot(test, l c V { p ^, label=\'SARIMA\') plt.legend(loc=\'bess 0 [ . ) u %t\') plt.s6 M # 7 lt-winter 季节性预测算法预测 2019-07-23 到 2019-095 o u p ^-23 期间的每日广告支出,代码如下:

    from statsmodels.tsa.api import ExpoJ @ g u *nentialSmoothing fit1 = ExponentialSmoothing(np.asarray(train) ,seasonal_periods=7 ,trend=\'add\'O H c v, seasonal=\'add\').fit(use_boxcox=T} ` H f U 5 h _rue)test% f _ - = * K o = fit1.forecast(len(test)) plt.figure(figsize=(16,8)) plt.plot( train, labe= + : E J !l=\'Tr: z c Wain\') plt.plot(test, label=\'; % v Q %Test\') plt.plot(test, label=\'Holt_{ u FWiT _ - 1 e g 2 V tnter\') plt.legend(loc=\'best\') plt.show


    通过 Python 代码实现时间序列数据的统计学预测模型

    同样,我们通过 mean_squared_error,mean_absolute_error函数查看 mse 和 mae。

    通过 Python 代码实现时间序列数据的统计学预测模型


    通过 Python 代码实现时间序列数据的统计学预测模型

    可以看到,H-W 模型同样能够预测时间序列变化趋N K | a 9 ) F y X势和季节性特征。

    通过 Python 代码实现时间序列数据的统计学预测模型

    算法对比

    通过将两种算法的预测结果进行对比,可以评价哪种方法预测能力更好。

    通过 Python 代码实现时间序列数据的统计学预测模型

    从图中可以看出,在MSE和MAE均较低的情况下,SARIMA模型的性能略优于Holt-Winter模型。尽管这两种模式都无法完美地抓住峰谷特征,但它们仍然对企业有用。根据数据,平均每月广告支出为2百万美元以上。而这两种算法的MAE大约在6000左右。换言之,对于一家平均每月广告支出为2百万美元的企业,两个月的广告支出预测误差只在6000美元左右,这是相当可z L = M i观的。

    通过 Python 代码实现时间序列数据的统计学预测模型

    结束语

    在本文中,单变量预测方法在广告支出数据上表现良好。但这些方法难以组合/合并新的E O :信号(如事件、天气)。同时这些方法对1 N p @ . #丢失数据也非常d # j敏感,通常不能很好地预测v ( { * 9很长一段时间。

    你还想了解深度学习` j b O | D [技术的哪些问题,戳文末评论告诉我F @ d 0 { w g - 7们,就有机会D o y获得「AI 开发者万人[ k X 6 [ O Z / :大会」的在线直播门票哦!

    通过 Python 代码实现时间序列数据的统计学预测模型


    • 前百度主= = w任架构师创业,c t b ;两年融6 % s z资千万美元,他说AI新药研发将迎来黄金十年
    • 北京四环堵车引发的智能交通大构想
    • 400 多行代码!超详细中文聊天机器人开发指南 | 原力计划
    • 三大运营商将上V | # r线 5G 消息;苹果谷歌联手,追踪 30 亿用户;j8 * y A T G p jQuery 3.5.0 发布 | 极客头条
    • 比特币当赎金,WannaRen 勒索病毒二度来袭!
    • 你公司的虚拟~ A $机还闲着?基于 Jenkins 和 Ku% i # R ~bernetes 的持续集成测试实践了解一下!
    • 从 Weu q } [ Sb 1.0到Web 3.0:详析这些年互联网的发展及未来方向


美元测试企业发展安远时间模型数据预测