将Python Statsmodel ARIMA模型参数改装为新数据并进行预测

问题描述 投票:1回答:1

我已经将拦截系数,AR,MA存储在statsmodel包的ARIMA模型中

x = df_sku
x_train = x['Weekly_Volume_Sales']

x_train_log = np.log(x_train)
x_train_log[x_train_log == -np.inf] = 0
x_train_mat = x_train_log.as_matrix()

model = ARIMA(x_train_mat, order=(1,1,1))
model_fit = model.fit(disp=0)
res = model_fit.predict(start=1, end=137, exog=None, dynamic=False)
print(res)
params = model_fit.params

但我无法找到有关statsmodel的任何文档,它允许我将模型参数重新设置为一组新数据并预测N个步骤。

有没有人能够完成改装模型并预测出时间样本?

我正在尝试完成与R类似的事情:

# Refit the old model with testData
new_model <- Arima(as.ts(testData.zoo), model = old_model)
python python-3.x statsmodels arima
1个回答
1
投票

这是您可以使用的代码:

def ARIMAForecasting(data, best_pdq, start_params, step):
    model = ARIMA(data, order=best_pdq)
    model_fit = model.fit(start_params = start_params)
    prediction = model_fit.forecast(steps=step)[0]
    #This returns only last step
    return prediction[-1], model_fit.params

#Get the starting parameters on train data
best_pdq = (3,1,3) #It is fixed, but you can search for the best parameters

model = ARIMA(train_data, best_pdq)
model_fit = model.fit()
start_params = model_fit.params

data = train_data
predictions = list()
for t in range(len(test_data)):
    real_value = data[t]
    prediction = ARIMAForecasting(data, best_pdq, start_params)
    predictions.append(prediction)
    data.append(real_value)
#After you can compare test_data with predictions

您可以在这里查看详细信息:https://www.statsmodels.org/dev/generated/statsmodels.tsa.arima_model.ARIMA.fit.html#statsmodels.tsa.arima_model.ARIMA.fit

© www.soinside.com 2019 - 2024. All rights reserved.