import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from matplotlib.pylab import rcParams
rcParams["figure.figsize"] = 15,6
#读取数据
f = open(r"D:\miss_predict.txt")
data = pd.read_table(f)
#时间序列索引
data_bs.index = pd.date_range(start='2018-08-01 00:00:00',periods=744,freq='h',normalize=True)
data = data_bs["指标值"]
#判断数据的平稳性
%pylab inline
plt.figure(figsize(20,8))
plt.plot(data)
#使用均值填充
#判断数据的稳定性
from statsmodels.tsa.stattools import adfuller
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
# 移动平均图
def draw_trend(timeSeries, size):
f = plt.figure(facecolor='white')
# 对size个数据进行移动平均
rol_mean = timeSeries.rolling(window=size).mean()
# 对size个数据进行加权移动平均
rol_weighted_mean = pd.ewma(timeSeries, span=size)
timeSeries.plot(color='blue', label='Original')
rol_mean.plot(color='red', label='Rolling Mean')
rol_weighted_mean.plot(color='black', label='Weighted Rolling Mean')
plt.legend(loc='best')
plt.title('Rolling Mean')
plt.show()
def draw_ts(timeSeries):
f = plt.figure(facecolor='white')
timeSeries.plot(color='blue')
plt.show()
'''
Unit Root Test
The null hypothesis of the Augmented Dickey-Fuller is that there is a unit
root, with the alternative that there is no unit root. That is to say the
bigger the p-value the more reason we assert that there is a unit root
'''
def testStationarity(ts):
dftest = adfuller(ts)
# 对上述函数求得的值进行语义描述
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
for key,value in dftest[4].items():
dfoutput['Critical Value (%s)'%key] = value
return dfoutput
# 自相关和偏相关图,默认阶数为31阶
def draw_acf_pacf(ts, lags=50):
f = plt.figure(facecolor='white')
ax1 = f.add_subplot(211)
plot_acf(ts, lags=50, ax=ax1)
ax2 = f.add_subplot(212)
plot_pacf(ts, lags=50, ax=ax2)
plt.show()
p值没有小于0.05,且ACF/PACF图不平稳
分解,季节性模型:
建立模型:
预测:
诊断异常:
上述内容,只是个大概的思路,并没有详细叙述,后续有机会填补。另外想要详细内容,请参考下面文章或者评论区讨论。
http://www.statsmodels.org/stable/examples/index.html#regression
https://www.cnblogs.com/bradleon/p/6832867.html
https://blog.csdn.net/u014281392/article/details/77585419/
https://www.cnblogs.com/foley/p/5582358.html
http://www.lizenghai.com/archives/595.html
https://www.howtoing.com/a-guide-to-time-series-forecasting-with-arima-in-python-3/
还有相关的论文,如: