【发布时间】:2022-01-18 04:06:03
【问题描述】:
目前我正在尝试使用外部变量在 python 中构建 SARIMAX 模型。 不幸的是,我收到此错误:“无法使用灵活类型执行减少”
# Function for Rolling Forecast with Sarima
def rolling_forecast(traindata,test_data, Modell_order = None , Sarima_order = None, eliminate_0 = True, exogen= None, exogen_test=None):
history = [x for x in traindata]
history_exogen = [x for x in exogen]
predictions = list()
for t in range(len(test_data)):
Sarima_Modell_same = SARIMAX(history,order = Modell_order ,seasonal_order= Sarima_order, exog=history_exogen)
model_fit = Sarima_Modell_same.fit()
output = model_fit.forecast(steps = 1,exog=history_exogen)
yhat = output[0]
obs = test_data[t]
obs_ex = exogen_test[t]
predictions.append(yhat)
history.append(obs)
history_exogen.append(obs_ex)
#print('predicted=%f, expected=%f' % (yhat, obs))
series_predicted = pd.Series(predictions, dtype='float64')
series_predicted.index = test_data.index
if eliminate_0 is True:
# Eliminate 0 values --> (for differenced Time Series not applyable because of negativ values)
series_predicted = series_predicted.apply(lambda x : x if x > 0 else 0)
test_data.plot()
series_predicted.plot(color = 'red')
else:
test_data.plot()
series_predicted.plot(color = 'red')
#print(sqrt(mean_squared_error(test_data, series_predicted)))
Is there any way to do this?
【问题讨论】:
-
欢迎来到 Stack Overflow。这可能是您练习调试技能的好时机。以下三个参考资料为调试代码提供了极好的建议。 How to debug small programs、Six Debugging Techniques for Python Programmers 或 Ultimate Guide to Python Debugging
-
我已经使用 SARIMA 实现了单变量预测,但使用 SARIMAX 模型我不知道如何修复它。
标签: python time-series statsmodels forecasting