簡體   English   中英

將 Python Statsmodel ARIMA 模型參數改裝為新數據並進行預測

[英]Refit Python Statsmodel ARIMA model parameters to new data and predict

我已經存儲了 statsmodel 包的 ARIMA 模型的截距、AR、MA 系數

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)

這是您可以使用的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM