Wednesday 14 July 2021

Python - How to get confidence interval in Keras models?

I currently have a Keras LSTM model set up for a timeseries forecast. I have made fitted data to the model and made some predictions. I have also outputted the RMSE values (if that helps).

How do I calculate the confidence interval for my prediction? Thank you.

def fit_model(train, batch_size, num_epochs, neurons):
  X, y = train[:, 0:-1], train[:, -1]
  X = X.reshape(X.shape[0], 1, X.shape[1])
  model = Sequential()
  model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
  model.add(Dense(1))
  model.compile(loss='mean_squared_error', optimizer='adam')
  for i in range(num_epochs):
      model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
      model.reset_states()
  return model

error_scores = list()
repeats = 5
for r in range(repeats):
    # fit the model
    batch_size = 1
    epochs = 200
    model = fit_model(train_scaled, batch_size, epochs, neurons)
    # forecast the entire training dataset to build up state for forecasting
    train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
    model.predict(train_reshaped, batch_size=batch_size)
    # forecast test dataset
    test_reshaped = test_scaled[:, 0].reshape(len(test_scaled), 1, 1)
    output = model.predict(test_reshaped, batch_size=batch_size)
    predictions = list()
    for i in range(len(output)):
        # make one-step forecast
        yhat = output[i,0]
        X = test_scaled[i, 0:-1]
        # invert scaling
        yhat = invert_scale(scaler, X, yhat)
        # invert differencing
        yhat = inverse_difference_series(raw_values, yhat, len(test_scaled)+1-i)
        # store forecast
        predictions.append(yhat)
   
      rmse = sqrt(mean_squared_error(raw_values[-80:], predictions))
      print('%d) Test RMSE: %.3f' % (r+1, rmse))
      error_scores.append(rmse)


from Python - How to get confidence interval in Keras models?

No comments:

Post a Comment