使用Python中的每小时数据的SARIMA

时间:2019-08-08 12:35:24

标签: python time-series arima

我是时间序列的新手,想使用4年(2015-2018年)的每小时负荷数据进行一些预测。有人可以帮我写订单和季节性订单吗?

到目前为止,我已经写了每月的模型,我也想每小时更改或显示一次。

mod = sm.tsa.statespace.SARIMAX(y,
                                order=(0, 0, 1),
                                seasonal_order=(1, 1, 1, 12),
                                enforce_stationarity=False,
                                enforce_invertibility=False)
results = mod.fit()
print(results.summary().tables[1])

results.plot_diagnostics(figsize=(18, 8))
plt.show()

1 个答案:

答案 0 :(得分:2)

您可以使用此方法为orderseasonal_order生成值最少为AIC的值。如果您知道p,d和q的范围,则可以对其进行自定义。

import statsmodels.api as sm
import warnings
import itertools
# Define the d and q parameters to take any value between 0 and 1
q = d = range(0, 1)
# Define the p parameters to take any value between 0 and 3
p = range(0, 2)

# Generate all different combinations of p, q and q triplets
pdq = list(itertools.product(p, d, q))

# Generate all different combinations of seasonal p, q and q triplets
seasonal_pdq = [(x[0], x[1], x[2], 12) for x in list(itertools.product(p, d, q))]

warnings.filterwarnings("ignore") # specify to ignore warning messages
i = 0
AIC = []
SARIMAX_model = []
for param in pdq:
    for param_seasonal in seasonal_pdq:
        try:
            i+=1
            print('The iteration',i)
            print('length of pdq',len(pdq))
            print('length of seasonalpdq',len(seasonal_pdq))
            mod = sm.tsa.statespace.SARIMAX(train_data,
                                            order=param,
                                            seasonal_order=param_seasonal,
                                            enforce_stationarity=False,
                                            enforce_invertibility=False)

            results = mod.fit()

            print('SARIMAX{}x{} - AIC:{}'.format(param, param_seasonal, results.aic), end='\r')
            AIC.append(results.aic)
            SARIMAX_model.append([param, param_seasonal])
        except:
            continue

print('The smallest AIC is {} for model SARIMAX{}x{}'.format(min(AIC), SARIMAX_model[AIC.index(min(AIC))][0],SARIMAX_model[AIC.index(min(AIC))][1]))

# Let's fit this model
mod = sm.tsa.statespace.SARIMAX(train_data,
                                order=SARIMAX_model[AIC.index(min(AIC))][0],
                            seasonal_order=SARIMAX_model[AIC.index(min(AIC))][1],
                                enforce_stationarity=False,
                                enforce_invertibility=False)