Error-Based Metrics
- Error-based metrics measure the difference between predicted and actual values to evaluate the accuracy of regression models.
Mean Absolute Error (MAE)
MAE calculates the average of absolute differences between actual and predicted values.
MAE=1n∑i=1n∣yi−y^i∣MAE = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|MAE=n1i=1∑n∣yi−y^i∣
Where:
yiy_iyi = Actual value
y^i\hat{y}_iy^i = Predicted value
nnn = Number of observations
Characteristics
Measures average magnitude of errors
Simple to understand
Less sensitive to outliers than MSE
Example
Actual = [3, 5, 2]
Predicted = [2, 4, 3]
MAE=∣3−2∣+∣5−4∣+∣2−3∣3=1+1+13=1MAE = \frac{|3-2| + |5-4| + |2-3|}{3} = \frac{1+1+1}{3} = 1MAE=3∣3−2∣+∣5−4∣+∣2−3∣=31+1+1=1
Mean Squared Error (MSE)
MSE calculates the average of squared differences between actual and predicted values.
MSE=1n∑i=1n(yi−y^i)2MSE = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2MSE=n1i=1∑n(yi−y^i)2
Characteristics
Penalizes larger errors more heavily than MAE
Sensitive to outliers
Often used in optimization (gradient descent)
Example
Actual = [3, 5, 2]
Predicted = [2, 4, 3]
MSE=(3−2)2+(5−4)2+(2−3)23=1+1+13=1MSE = \frac{(3-2)^2 + (5-4)^2 + (2-3)^2}{3} = \frac{1+1+1}{3} = 1MSE=3(3−2)2+(5−4)2+(2−3)2=31+1+1=1
Root Mean Squared Error (RMSE)
RMSE is the square root of MSE, giving error in the same units as target variable.
RMSE=MSE=1n∑i=1n(yi−y^i)2RMSE = \sqrt{MSE} = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2}RMSE=MSE=n1i=1∑n(yi−y^i)2
Characteristics
Gives intuitive interpretation in original units
Penalizes large errors more than MAE
Example
MSE = 1
RMSE=1=1RMSE = \sqrt{1} = 1RMSE=1=1
Example: MAE, MSE, RMSE
MAE, MSE, and RMSE Calculation in Python for Model Evaluation
This Python example demonstrates how to evaluate a machine learning model using common regression error metrics: Mean Absolute Error (MAE), Mean Squared Error (MSE), and Root Mean Squared Error (RMSE). The code compares actual and predicted values, calculates each metric using scikit-learn and NumPy, and prints the results to measure the prediction accuracy of the model.
# Step 1: Import Libraries
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np
# Step 2: Actual vs Predicted
y_true = np.array([3, 5, 2])
y_pred = np.array([2, 4, 3])
# Step 3: Calculate MAE
mae = mean_absolute_error(y_true, y_pred)
print("Mean Absolute Error (MAE):", mae)
# Step 4: Calculate MSE
mse = mean_squared_error(y_true, y_pred)
print("Mean Squared Error (MSE):", mse)
# Step 5: Calculate RMSE
rmse = np.sqrt(mse)
print("Root Mean Squared Error (RMSE):", rmse)
Output:
Mean Absolute Error (MAE): 1.0
Mean Squared Error (MSE): 1.0
Root Mean Squared Error (RMSE): 1.0