R2 Score vs MSE vs MAE in Python: Key Differences and Usage
R2 score measures how well a regression model explains the variance of the target variable, with values closer to 1 being better. MSE (Mean Squared Error) calculates the average squared difference between predicted and actual values, penalizing larger errors more. MAE (Mean Absolute Error) computes the average absolute difference, giving a straightforward measure of average error size.Quick Comparison
Here is a quick comparison of R2 score, MSE, and MAE based on key factors:
| Metric | Range | Interpretation | Error Sensitivity | Units | Goal |
|---|---|---|---|---|---|
| R2 Score | (-∞, 1] | Higher is better; 1 means perfect fit | Sensitive to variance explained | Unitless | Maximize |
| MSE (Mean Squared Error) | [0, ∞) | Lower is better; penalizes large errors more | High (squares errors) | Squared units of target | Minimize |
| MAE (Mean Absolute Error) | [0, ∞) | Lower is better; average absolute error | Moderate (linear errors) | Same units as target | Minimize |
Key Differences
R2 score shows how much of the target's variance your model explains. It is a relative measure and can be negative if the model is worse than predicting the mean. It is unitless and easy to interpret as a percentage of explained variance.
MSE calculates the average of squared differences between predicted and actual values. Squaring means larger errors weigh more heavily, making it sensitive to outliers. Its units are squared compared to the target variable, which can be harder to interpret directly.
MAE computes the average absolute difference between predictions and true values. It treats all errors equally without squaring, so it is more robust to outliers and easier to understand since it shares the same units as the target variable.
Code Comparison
Example of calculating R2 score in Python using sklearn:
from sklearn.metrics import r2_score y_true = [3, -0.5, 2, 7] y_pred = [2.5, 0.0, 2, 8] r2 = r2_score(y_true, y_pred) print(f"R2 Score: {r2:.3f}")
MSE and MAE Equivalent
Equivalent code to calculate MSE and MAE using sklearn:
from sklearn.metrics import mean_squared_error, mean_absolute_error y_true = [3, -0.5, 2, 7] y_pred = [2.5, 0.0, 2, 8] mse = mean_squared_error(y_true, y_pred) mae = mean_absolute_error(y_true, y_pred) print(f"Mean Squared Error: {mse:.3f}") print(f"Mean Absolute Error: {mae:.3f}")
When to Use Which
Choose R2 score when you want to understand how well your model explains the variability of the data relative to a simple average prediction. Use MSE if you want to heavily penalize large errors, which is useful when big mistakes are especially bad. Opt for MAE when you want a clear, interpretable average error size that treats all errors equally and is robust to outliers.