ARIMA 小白实战初探

记录利用时间序列模型 ARIMA(p,d,q)进行顾客满意度的预测。

ARIMA模型建立流程:

  • 平稳序列(差分法确定d)
  • p和q阶数的确定(ACF和PACF)
  • ARIMA(p,d,q)

模型

导入库并设置一些画图参数

from __future__ import absolute_import, division, print_function

%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format='retina'

# http://www.lfd.uci.edu/~gohlke/pythonlibs/#xgboost
import sys
import os

import pandas as pd
import numpy as np

# # Remote Data Access
# import pandas_datareader.data as web
# import datetime
# # reference: https://pandas-datareader.readthedocs.io/en/latest/remote_data.html

# TSA from Statsmodels
import statsmodels.api as sm
import statsmodels.formula.api as smf
import statsmodels.tsa.api as smt

# Display and Plotting
import matplotlib.pylab as plt
import seaborn as sns

pd.set_option('display.float_format', lambda x: '%.5f' % x) # pandas
np.set_printoptions(precision=5, suppress=True) # numpy

pd.set_option('display.max_columns', 100)
pd.set_option('display.max_rows', 100)

# seaborn plotting style
sns.set(style='ticks', context='poster')

读入数据,并建立dataframe,画图看一下走势

#Read the data
#美国消费者信心指数
Sentiment = 'data/sentiment.csv'
Sentiment = pd.read_csv(Sentiment, index_col=0, parse_dates=[0])

# Select the series from 2005 - 2016
sentiment_short = Sentiment.loc['2005':'2016']
#画图
sentiment_short.plot(figsize=(12,8))
plt.legend(bbox_to_anchor=(1.25, 0.5))
plt.title("Consumer Sentiment")
sns.despine()

在这里插入图片描述
可以发现数据的波动很大,所以此时需要对数据进行差分处理。

差分法确定d

#差分法
sentiment_short['diff_1'] = sentiment_short['UMCSENT'].diff(1)  #1个时间间隔

sentiment_short['diff_2'] = sentiment_short['diff_1'].diff(1) #二阶差分

sentiment_short.plot(subplots=True, figsize=(18, 12))

在这里插入图片描述

画PCF和PACF确定p和q,确定准则如图:
在这里插入图片描述
大致来说就是AR( p) 看PACF;MA(q) 看ACF

del sentiment_short['diff_2']
del sentiment_short['diff_1']
sentiment_short.head()
print (type(sentiment_short))

fig = plt.figure(figsize=(12,8))


#画ACF和PACF
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(sentiment_short, lags=20,ax=ax1)
ax1.xaxis.set_ticks_position('bottom')
fig.tight_layout();

ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(sentiment_short, lags=20, ax=ax2)
ax2.xaxis.set_ticks_position('bottom')
fig.tight_layout()

#阴影表示置信区间

在这里插入图片描述

所以p取9,q取3。总结上面的功能,此处给出一个模板用于分析和评估:

# 更直观一些的分析和评估(模板)
#用的时候只需要将数据和参数值传进来就好了

def tsplot(y, lags=None, title='', figsize=(14, 8)):
   
    fig = plt.figure(figsize=figsize)
    layout = (2, 2)
    ts_ax   = plt.subplot2grid(layout, (0, 0))
    hist_ax = plt.subplot2grid(layout, (0, 1))
    acf_ax  = plt.subplot2grid(layout, (1, 0))
    pacf_ax = plt.subplot2grid(layout, (1, 1))
    
    y.plot(ax=ts_ax)
    ts_ax.set_title(title)
    y.plot(ax=hist_ax, kind='hist', bins=25)
    hist_ax.set_title('Histogram')
    smt.graphics.plot_acf(y, lags=lags, ax=acf_ax)
    smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax)
    [ax.set_xlim(0) for ax in [acf_ax, pacf_ax]]
    sns.despine()
    plt.tight_layout()
    return ts_ax, acf_ax, pacf_ax

在这里插入图片描述

过程中的error

  1. 出现DataFrame object has no attribute ‘as_matrix’
    原因:新库中没有as_matrix
    操作:df.as_matrix()改写成了df.values
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值