Regression Evaluation Metrics
Regression Evaluation Metrics
Measuring how well your regression model predicts continuous values.
What are Regression Evaluation Metrics?
Regression Evaluation Metrics are mathematical measures used to evaluate the performance of regression models by comparing the predicted values with the actual values.
Why Are They Important?
- Measure how accurate the predictions are.
- Help compare different regression models.
- Identify if the model is overfitting or underfitting.
- Provide insights for improving model performance.
- Help select the best model for deployment.
Notation Used
- yᵢ = actual value
- ŷᵢ = predicted value
- ȳ = mean of actual values
- n = total number of samples
Popular Regression Evaluation Metrics
Mean Absolute Error (MAE)
Average of the absolute differences between actual and predicted values.
from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_test, y_pred)
print("MAE:", mae)
Mean Squared Error (MSE)
Average of the squared differences between actual and predicted values.
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test, y_pred)
print("MSE:", mse)
Root Mean Squared Error (RMSE)
Square root of MSE — brings the error back to the same units as the target.
import numpy as np
rmse = np.sqrt(mse)
print("RMSE:", rmse)
R² Score (Coefficient of Determination)
Measures how well the model explains the variance in the target variable.
Range: 0 → 1 (higher is better)
from sklearn.metrics import r2_score
r2 = r2_score(y_test, y_pred)
print("R²:", r2)
| R² Value | Meaning |
|---|---|
| 1.0 | Perfect prediction |
| 0.9 - 0.99 | Excellent model |
| 0.7 - 0.9 | Good model |
| 0.5 - 0.7 | Average model |
| 0 - 0.5 | Poor model |
| Negative | Worse than the mean baseline |
Adjusted R² Score
Improved version of R² that adjusts for the number of input features.
Where:
- n = number of samples
- p = number of features
Mean Absolute Percentage Error (MAPE)
Expresses the prediction error as a percentage of actual values.
import numpy as np
def mape(y_true, y_pred):
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
print("MAPE:", mape(y_test, y_pred))
Median Absolute Error (MedAE)
Uses the median of absolute errors — robust to outliers.
from sklearn.metrics import median_absolute_error
medae = median_absolute_error(y_test, y_pred)
print("MedAE:", medae)
Mean Squared Logarithmic Error (MSLE)
Like MSE but uses logarithms — penalizes underestimation more than overestimation.
from sklearn.metrics import mean_squared_log_error
msle = mean_squared_log_error(y_test, y_pred)
print("MSLE:", msle)
Comparison of All Metrics
| Metric | Range | Outlier Sensitive? | Best For |
|---|---|---|---|
| MAE | 0 → ∞ | Low | Simple error interpretation |
| MSE | 0 → ∞ | High | Penalizing large errors |
| RMSE | 0 → ∞ | High | Common reporting metric |
| R² | −∞ → 1 | Medium | Variance explanation |
| Adjusted R² | −∞ → 1 | Medium | Multiple features |
| MAPE | 0 → ∞ % | Low | % based interpretation |
| MedAE | 0 → ∞ | Very Low | Robust to outliers |
| MSLE | 0 → ∞ | Low | Exponential trends |
Full Python Example
pip install pandas numpy scikit-learn
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import (
mean_absolute_error, mean_squared_error,
r2_score, median_absolute_error
)
# Sample dataset
X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
y = np.array([10, 20, 25, 40, 45, 60, 70, 85, 90, 100])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print("MAE:", mean_absolute_error(y_test, y_pred))
print("MSE:", mean_squared_error(y_test, y_pred))
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
print("R²:", r2_score(y_test, y_pred))
print("MedAE:", median_absolute_error(y_test, y_pred))
When to Use Which Metric?
- Use MAE when you want a simple, interpretable metric.
- Use MSE/RMSE when large errors are unacceptable.
- Use R² to understand how well variance is explained.
- Use Adjusted R² for multiple linear regression.
- Use MAPE when reporting % errors to business teams.
- Use MedAE when dataset contains outliers.
- Use MSLE for exponentially growing data.
Real-Life Analogy
Evaluation Metrics = Exam Score
Just like exam marks measure how well a student understood a subject, regression metrics measure how well a model has learned to predict. Different metrics highlight different aspects of performance.
Common Mistakes to Avoid
Best Practices
Quick Tips
- Use multiple metrics instead of one.
- Always test on unseen data.
- Visualize predictions vs actual values.
- Use cross-validation for stable results.
- Track performance metrics across multiple runs.
- Choose metric based on the business context.
Importance of Regression Metrics
Measure Accuracy
- Compares predictions to reality
- Tells how good the model is
Compare Models
- Find best performing model
- Helps with model selection
Detect Overfitting
- Train vs test gap
- Identifies weakness
Business Insight
- Helps communicate results
- Supports data decisions
Golden Rule
Key Takeaway
Regression Evaluation Metrics like MAE, MSE, RMSE, R², and MAPE are essential tools for measuring how well a regression model performs. Using the right combination of metrics gives you a clear understanding of your model's accuracy, reliability, and business value.