'Hidden Markov Model - Rolling window prediction. Error: index 1 is out of bounds for axis 0 with size 1

I am making a Hidden Markov Model that predicts one value at a time (rolling window). However I keep getting an error every time I run my loop and try to save the predicted value.

The forecasted_variables inside the loop gives me the following error: "index 1 is out of bounds for axis 0 with size 1". I tried making forecasted_variables an array with random numbers with the size I need (1008,4). I also tried making it an empty list and appending the values I need but I get the same error.

In every iteration I am updating the training data in the variable called history. The loop should run 1008 times and each time save the predicted values inside forecasted_variables.

###Rolling window

forecasted_activepower=[]
forecasted_variables=[]
test_activepower=test_data[:,0]
train_activepower=features[:,0]
features_model = GaussianHMM(n_components=4)
history = features.tolist()
for t in range(test_activepower.shape[0]):
    features_model.fit(history)
    forecast,pred_states=features_model.sample(1) #forecast holds the prediction for all the 
variables
    forecasted_variables=forecast[t,:]
    forecasted_activepower=forecast[t,0]
    history.append(test_data[t,:]) # history is used as the data for the model.
print(forecasted_activepower)

Data before interpolations



Solution 1:[1]

Ok, I was able to fix it. My mistake was that I was indexing forecast (which only had one row of values for every iteration). I fixed it by appending forecast to my variables.

    forecasted_variables.append(forecast)
    forecasted_activepower.append(forecast[0,0])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brucee