Guest User

Untitled

a guest
Apr 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. def calculate_forecast_errors(df, prediction_size):
  2. """Calculate MAPE and MAE of the forecast.
  3.  
  4. Args:
  5. df: joined dataset with 'y' and 'yhat' columns.
  6. prediction_size: number of days at the end to predict.
  7. """
  8.  
  9. # Make a copy
  10. df = df.copy()
  11.  
  12. # Now we calculate the values of e_i and p_i according to the formulas given in the article above.
  13. df['e'] = df['y'] - df['yhat']
  14. df['p'] = 100 * df['e'] / df['y']
  15.  
  16. # Recall that we held out the values of the last `prediction_size` days
  17. # in order to predict them and measure the quality of the model.
  18.  
  19. # Now cut out the part of the data which we made our prediction for.
  20. predicted_part = df[-prediction_size:]
  21.  
  22. # Define the function that averages absolute error values over the predicted part.
  23. error_mean = lambda error_name: np.mean(np.abs(predicted_part[error_name]))
  24.  
  25. # Now we can calculate MAPE and MAE and return the resulting dictionary of errors.
  26. return {'MAPE': error_mean('p'), 'MAE': error_mean('e')}
Add Comment
Please, Sign In to add comment