We use time series evaluation metrics to check how well our model predicts future values based on past data. This helps us know if our model is good or needs improvement.
Time series evaluation metrics in ML Python
Start learning this pattern below
Jump into concepts and practice - no test required
from sklearn.metrics import mean_absolute_error, mean_squared_error import numpy as np # y_true: actual values # y_pred: predicted values mae = mean_absolute_error(y_true, y_pred) mse = mean_squared_error(y_true, y_pred) rmse = np.sqrt(mse)
mean_absolute_error (MAE) measures average absolute difference between actual and predicted values.
mean_squared_error (MSE) squares the differences before averaging, penalizing bigger errors more.
root mean squared error (RMSE) is the square root of MSE, giving error in original units.
mae = mean_absolute_error([3, 5, 2], [2.5, 5, 2.1]) print(mae)
mse = mean_squared_error([3, 5, 2], [2.5, 5, 2.1]) print(mse)
rmse = np.sqrt(mean_squared_error([3, 5, 2], [2.5, 5, 2.1])) print(rmse)
This program compares actual and predicted time series values using MAE, MSE, and RMSE to evaluate prediction accuracy.
from sklearn.metrics import mean_absolute_error, mean_squared_error import numpy as np # Actual values of a time series actual = [100, 150, 200, 250, 300] # Predicted values from a model predicted = [110, 140, 210, 240, 310] # Calculate MAE mae = mean_absolute_error(actual, predicted) # Calculate MSE mse = mean_squared_error(actual, predicted) # Calculate RMSE rmse = np.sqrt(mse) print(f"MAE: {mae:.2f}") print(f"MSE: {mse:.2f}") print(f"RMSE: {rmse:.2f}")
Lower values of MAE, MSE, and RMSE mean better predictions.
RMSE is more sensitive to large errors than MAE.
Always compare metrics on the same dataset to judge model improvements.
Time series evaluation metrics help measure prediction errors.
MAE, MSE, and RMSE are common and easy to use.
Use these metrics to improve and trust your forecasting models.
Practice
Solution
Step 1: Understand the definition of MAE
MAE calculates the average of the absolute differences between predicted and actual values, showing average error size.Step 2: Compare with other metrics
MSE and RMSE square errors, while R-squared measures variance explained, not average error.Final Answer:
Mean Absolute Error (MAE) -> Option BQuick Check:
Average absolute difference = MAE [OK]
- Confusing MAE with MSE or RMSE
- Thinking R-squared measures error size
- Assuming RMSE is the same as MAE
Solution
Step 1: Recall RMSE formula
RMSE is the square root of the average of squared errors, so it must include squaring, averaging, then square root.Step 2: Check each option
RMSE = \(\sqrt{\frac{1}{n} \sum_{i=1}^n e_i^2}\): \(\sqrt{\frac{1}{n} \sum_{i=1}^n e_i^2}\) matches the formula exactly. RMSE = \(\sum_{i=1}^n e_i^2\) misses averaging and root. RMSE = \(\frac{1}{n} \sum_{i=1}^n |e_i|\) is MAE. RMSE = \(\frac{1}{n} \sum_{i=1}^n e_i\) is mean error (not squared).Final Answer:
RMSE = \(\sqrt{\frac{1}{n} \sum_{i=1}^n e_i^2}\) -> Option DQuick Check:
RMSE = sqrt(mean squared errors) [OK]
- Forgetting to take square root
- Using absolute errors instead of squared
- Not dividing by number of points
Solution
Step 1: Calculate errors and square them
Errors: 3-2=1, 5-5=0, 2-4=-2, 7-8=-1. Squared errors: 1, 0, 4, 1.Step 2: Compute average of squared errors
Sum = 1+0+4+1=6. Average = 6/4 = 1.5.Final Answer:
1.5 -> Option AQuick Check:
Sum squared errors / count = 1.5 [OK]
- Using absolute errors instead of squared
- Forgetting to average over all points
- Mixing predicted and actual values
def mae(actual, predicted):
errors = [a - p for a, p in zip(actual, predicted)]
return sum(errors) / len(errors)Solution
Step 1: Analyze error calculation
The code calculates errors as differences but does not take absolute values, which MAE requires.Step 2: Understand MAE definition
MAE is mean of absolute errors, so errors must be wrapped with abs() before summing.Final Answer:
Errors should be absolute values before summing -> Option CQuick Check:
MAE needs absolute errors [OK]
- Skipping absolute value in error calculation
- Dividing by wrong denominator
- Confusing MAE with MSE
Solution
Step 1: Interpret MAE and RMSE values
Model B has lower MAE but higher RMSE, meaning it has better average error but more large errors. Model A has lower RMSE, indicating fewer large errors.Step 2: Decide which metric matters more
RMSE penalizes large errors more, so lower RMSE often means more reliable predictions without big mistakes.Final Answer:
Model A, because lower RMSE means fewer large errors -> Option AQuick Check:
Lower RMSE means fewer big errors [OK]
- Choosing model with lower MAE ignoring RMSE
- Thinking higher RMSE is better
- Expecting MAE and RMSE to be equal
