Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def calculate_forecast_errors(df, prediction_size):
- """Calculate MAPE and MAE of the forecast.
- Args:
- df: joined dataset with 'y' and 'yhat' columns.
- prediction_size: number of days at the end to predict.
- """
- # Make a copy
- df = df.copy()
- # Now we calculate the values of e_i and p_i according to the formulas given in the article above.
- df['e'] = df['y'] - df['yhat']
- df['p'] = 100 * df['e'] / df['y']
- # Recall that we held out the values of the last `prediction_size` days
- # in order to predict them and measure the quality of the model.
- # Now cut out the part of the data which we made our prediction for.
- predicted_part = df[-prediction_size:]
- # Define the function that averages absolute error values over the predicted part.
- error_mean = lambda error_name: np.mean(np.abs(predicted_part[error_name]))
- # Now we can calculate MAPE and MAE and return the resulting dictionary of errors.
- return {'MAPE': error_mean('p'), 'MAE': error_mean('e')}
Add Comment
Please, Sign In to add comment