python - history.append() throws "IndexError: index 184 is out of bounds for axis 0 with size 184"
问题描述
This error was generated by history.append(observed)
.
Background:
I fed the UDF doForecast with price data of about 460 price point data - called "panel".
I imported MSE(Sklearn), Arima(Statsmodel) and Autocorrelation(pandas.plotting) with the follow parameters
- TRAIN_SIZE = 0.2
- FORECAST_STEPS = 20
I ran my entire code, however it stop after bounds;
Here's the error;
(1) line 142, in downloadData
print ('date={0}, predicted={1}, expected={2}, error pct={3:.2f}%'.format(histPanelWeekly.keys()[len(history)-1],
(2) line 271, in __getitem__
val = getitem(key)
(3) line 271, in __getitem__
val = getitem(key)
IndexError: index 184 is out of bounds for axis 0 with size 95
> def doProjection(panel):
>
> closeDF = panel['Close']
> ts = closeDF['SPY']
> tsWeekly = ts.resample('W-MON').last()
> values = tsWeekly.tolist()
> trainSize = int(ts.size * TRAIN_SIZE)
train, test = values[0:trainSize], values[trainSize:ts.size]
history = train
predictions = list()
errorPcts = list()
for t in range(len(test)):
model = ARIMA(history, order=(2, 1, 0))
modelFit = model.fit(disp=0)
output = modelFit.forecast()
predicted = output[0][0]
predictions.append(predicted)
observed = test[t]
history.append(observed)
errorPct = (predicted-observed)/observed * 100
errorPcts.append(errorPct)
print ('date={0}, predicted={1}, expected={2}, error pct={3:.2f}%'.format(tsWeekly.keys()[len(history)-1],
predicted, observed, errorPct))
error = mean_squared_error(test, predictions)
print ('Test MSE: {0:.3f}'.format(error))
pyplot.subplot(211)
pyplot.title('Predicted vs Expected')
pyplot.plot(test)
def main():
doForecast(panel)
if __name__ == '__main__':
main()
解决方案
推荐阅读
- python - “List[int]”类型的表达式不能分配给声明的类型“List[int | None]”
- python - 一组凸集的量子信息理论兴趣的最小和最大椭球是什么?
- c# - 验证单词“the”是否在我的字符串数组中
- microsoft-teams - Web 版本是否支持 Teams [侧面板] 会议应用程序?
- reactjs - mapboxGL 上的单击处理程序不响应使用带有打字稿的反应钩子的更新状态
- python - 如何在 python 中使用 asyncSSH 库通过 ssh 正确发送 ~m
- javascript - Javascript 检查字符串的一部分是否包含在枚举值中
- python - 将熊猫数据框放入嵌套子字典
- python - 在 Heroku 上部署 playwright-python
- logging - 如何在手动创建的记录器(dotnet core 3.1)中配置 LogLevel