首页 > 解决方案 > 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

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()

标签: pythonpython-3.xtime-seriesarima

解决方案


推荐阅读