0
0
MlopsComparisonBeginner · 4 min read

R2 Score vs MSE vs MAE in Python: Key Differences and Usage

In Python, 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:

MetricRangeInterpretationError SensitivityUnitsGoal
R2 Score(-∞, 1]Higher is better; 1 means perfect fitSensitive to variance explainedUnitlessMaximize
MSE (Mean Squared Error)[0, ∞)Lower is better; penalizes large errors moreHigh (squares errors)Squared units of targetMinimize
MAE (Mean Absolute Error)[0, ∞)Lower is better; average absolute errorModerate (linear errors)Same units as targetMinimize
⚖️

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:

python
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}")
Output
R2 Score: 0.923
↔️

MSE and MAE Equivalent

Equivalent code to calculate MSE and MAE using sklearn:

python
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}")
Output
Mean Squared Error: 0.375 Mean Absolute Error: 0.500
🎯

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.

Key Takeaways

R2 score measures explained variance and is unitless; higher values are better.
MSE penalizes larger errors more due to squaring and has squared units.
MAE gives average absolute error in the same units as the target and is robust to outliers.
Use R2 for overall fit quality, MSE to emphasize large errors, and MAE for straightforward average error.
All three metrics can be easily computed with sklearn for regression evaluation.