時(shí)間序列就是以時(shí)間為索引的數(shù)據(jù),比如下面這種形式數(shù)據(jù)鏈接:https://pan.baidu.com/s/1KHmCbk9ygIeRHn97oeZVMg
提取碼:s0k5
python使用ARIMA建模,主要是使用statsmodels庫(kù)
首先是建模流程,如果不是太明白不用擔(dān)心,下面會(huì)詳細(xì)的介紹這些過程

首先要注意一點(diǎn),ARIMA適用于短期 單變量預(yù)測(cè),長(zhǎng)期的預(yù)測(cè)值都會(huì)用均值填充,后面你會(huì)看到這種情況。
首先導(dǎo)入需要的包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.stats.diagnostic import acorr_ljungbox
from statsmodels.graphics.tsaplots import plot_pacf,plot_acf
載入數(shù)據(jù)
df=pd.read_csv('./附件1-區(qū)域15分鐘負(fù)荷數(shù)據(jù).csv',parse_dates=['數(shù)據(jù)時(shí)間'])
df.info()
將默認(rèn)索引改為時(shí)間索引
data=df.copy()
data=data.set_index('數(shù)據(jù)時(shí)間')
1 繪制時(shí)序圖
plt.plot(data.index,data['總有功功率(kw)'].values)
plt.show()
劃分訓(xùn)練集和測(cè)試集
train=data.loc[:'2018/1/13 23:45:00',:]
test=data.loc['2018/1/14 0:00:00':,:]
2 平穩(wěn)性檢驗(yàn)
# 單位根檢驗(yàn)-ADF檢驗(yàn)
print(sm.tsa.stattools.adfuller(train['總有功功率(kw)']))
1%、%5、%10不同程度拒絕原假設(shè)的統(tǒng)計(jì)值和ADF比較,ADF同時(shí)小于1%、5%、10%即說明非常好地拒絕該假設(shè),本數(shù)據(jù)中,adf結(jié)果為-5.22, 小于三個(gè)level的統(tǒng)計(jì)值,說明數(shù)據(jù)是平穩(wěn)的
3 白噪聲檢驗(yàn)
使用
Q
B
P
Q_{BP}
QBP? 和
Q
L
B
Q_{LB}
QLB? 統(tǒng)計(jì)量進(jìn)行序列的隨機(jī)性檢驗(yàn)
# 白噪聲檢驗(yàn)
acorr_ljungbox(train['總有功功率(kw)'], lags = [6, 12],boxpierce=True)
各階延遲下LB和BP統(tǒng)計(jì)量的P值都小于顯著水平(
α
=
0.05
\alpha=0.05
α=0.05),所以拒絕序列為純隨機(jī)序列的原假設(shè),認(rèn)為該序列為非白噪聲序列
4 計(jì)算ACF,PACF
# 計(jì)算ACF
acf=plot_acf(train['總有功功率(kw)'])
plt.title("總有功功率的自相關(guān)圖")
plt.show()
# PACF
pacf=plot_pacf(train['總有功功率(kw)'])
plt.title("總有功功率的偏自相關(guān)圖")
plt.show()
5 選擇合適的模型進(jìn)行擬合
ACF | PACF | 模型 |
---|---|---|
拖尾 | 截尾 | AR |
截尾 | 拖尾 | MA |
拖尾 | 拖尾 | ARMA |
如果說自相關(guān)圖拖尾,并且偏自相關(guān)圖在p階截尾時(shí),此模型應(yīng)該為AR(p )。
如果說自相關(guān)圖在q階截尾并且偏自相關(guān)圖拖尾時(shí),此模型應(yīng)該為MA(q)。
如果說自相關(guān)圖和偏自相關(guān)圖均顯示為拖尾,那么可結(jié)合ACF圖中最顯著的階數(shù)作為q值,選擇PACF中最顯著的階數(shù)作為p值,最終建立ARMA(p,q)模型。
從ACF和PACF圖的結(jié)果來看,p=7,q=4
model = sm.tsa.arima.ARIMA(train,order=(7,0,4))
arima_res=model.fit()
arima_res.summary()
因?yàn)榭醋韵嚓P(guān)圖和偏自相關(guān)圖有很大的主觀性,因此,可以通過AIC或BIC來確定最合適的階數(shù)
trend_evaluate = sm.tsa.arma_order_select_ic(train, ic=['aic', 'bic'], trend='n', max_ar=20,
max_ma=5)
print('train AIC', trend_evaluate.aic_min_order)
print('train BIC', trend_evaluate.bic_min_order)
6 模型預(yù)測(cè)
predict=arima_res.predict("2018/1/14 0:00:00","2018/1/14 23:45:00")
plt.plot(test.index,test['總有功功率(kw)'])
plt.plot(test.index,predict)
plt.legend(['y_true','y_pred'])
plt.show()
print(len(predict))
7 模型評(píng)價(jià)
from sklearn.metrics import r2_score,mean_absolute_error
mean_absolute_error(test['總有功功率(kw)'],predict)
8 殘差分析
res=test['總有功功率(kw)']-predict
residual=list(res)
plt.plot(residual)
查看殘差的均值是否在0附近
np.mean(residual)
殘差正態(tài)性檢驗(yàn)
import seaborn as sns
from scipy import stats
plt.figure(figsize=(10,5))
ax=plt.subplot(1,2,1)
sns.distplot(residual,fit=stats.norm)
ax=plt.subplot(1,2,2)
res=stats.probplot(residual,plot=plt)
plt.show()
在開頭說過,ARIMA不適用長(zhǎng)期預(yù)測(cè),下面把預(yù)測(cè)范圍調(diào)大,看看是否和文章開頭所說的一致文章來源:http://www.zghlxwxcb.cn/news/detail-797630.html
predict=arima_res.predict("2018/1/14 0:00:00","2018/1/18 23:45:00")
plt.plot(range(len(predict)),predict)
plt.legend(['y_true','y_pred'])
plt.show()
print(len(predict))
文章來源地址http://www.zghlxwxcb.cn/news/detail-797630.html
到了這里,關(guān)于python使用ARIMA進(jìn)行時(shí)間序列的預(yù)測(cè)(基礎(chǔ)教程)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!