Sub topic 6
R squared: how much better than the baseline?
This chapter assumes you can compute residuals with NumPy and know that the mean of y_true is a valid constant prediction. It also assumes you know what MAE and RMSE measure. Here you will learn what R squared (the coefficient of determination) measures and how to compute and read it.
The idea: compare against a baseline
MAE and RMSE are in the units of the target. An RMSE of 0.94 is hard to judge without context. Is that good for a target that ranges from 2 to 7? Would it still be good if the target ranged from 0 to 1000?
R squared answers a different question: how much of the target's variation does the model explain, compared with always predicting the mean? It is a unitless score, so it is easier to read across problems.
The formula
R squared compares two sums of squares.
SS_res(residual sum of squares) is the total squared error of your model: the sum of(y_true - y_pred)².SS_tot(total sum of squares) is the total squared error of the mean baseline: the sum of(y_true - mean(y_true))².
The score is:
R² = 1 - SSres / SStot
The ratio SS_res / SS_tot is the share of the baseline's error that your model still makes. Subtracting it from 1 gives the share the model removed.
Worked example
Use the four-point data from earlier chapters: y_true = [3.0, 5.0, 2.5, 7.0] and y_pred = [2.5, 5.0, 4.0, 8.0].
import numpy as np
y_true = np.array([3.0, 5.0, 2.5, 7.0])
y_pred = np.array([2.5, 5.0, 4.0, 8.0])
ss_res = np.sum((y_true - y_pred) ** 2) # 3.5
ss_tot = np.sum((y_true - y_true.mean()) ** 2) # 12.6875
r2 = 1 - ss_res / ss_tot # about 0.7241
print(ss_res, ss_tot, r2)The mean of y_true is 4.375. The baseline's squared errors add up to 12.6875. The model's add up to 3.5. The model keeps about 27.6% of the baseline's error, so R² is about 0.724. You can say the model removes about 72% of the squared error that the baseline makes.
Anchoring the scale
Three reference points help you read any R² value.
| R² | Meaning |
|---|---|
| 1.0 | Perfect predictions. `SS_res` is 0. |
| 0.0 | No better than predicting the mean. |
| Below 0 | Worse than predicting the mean. |
The zero point is not an accident. The baseline scores exactly 0 by construction, which you can check directly.
import numpy as np
y_true = np.array([3.0, 5.0, 2.5, 7.0])
# Always predicting the mean of y_true gives R squared of exactly 0.
y_pred = np.full_like(y_true, y_true.mean())
ss_res = np.sum((y_true - y_pred) ** 2)
ss_tot = np.sum((y_true - y_true.mean()) ** 2)
print(1 - ss_res / ss_tot) # 0.0Here SS_res and SS_tot are the same number, so the ratio is 1 and the score is 0.
What R squared does not tell you
- It is not a percentage of correct predictions. An R² of 0.72 does not mean 72% of predictions are right. It describes squared error reduction.
- It depends on the data's spread. The same model, with the same RMSE, gets a higher R² on data where the target varies widely, because
SS_totis larger. Compare R² values only on the same target and dataset. - It does not give the size of a typical miss. Report MAE or RMSE for that, since they carry the target's units.
- It inherits the outlier sensitivity of squared error. One large miss can dominate
SS_res.
Chapter 9 covers further pitfalls, including negative values on held-out data and what happens when you add features.
Common errors
- Swapping the arguments in scikit-learn.
r2_score(y_true, y_pred)is not symmetric. Passingy_predfirst changes the result, becauseSS_totis then computed from the predictions. Always pass the true values first. - Dividing by zero. If every
y_truevalue is identical,SS_totis 0 and R² is undefined. Scikit-learn handles this case specially rather than raising an error, so check your data if you see an unexpected value. - Treating R² as a pass/fail threshold. A "good" R² depends on the field. A value of 0.3 may be useful for noisy human-behavior data and poor for a physics measurement.
- Reading R² as proof of a good model. A high R² on training data says little about new data. Evaluate on held-out data.
Confirming with scikit-learn
The built-in function should match the hand calculation.
from sklearn.metrics import r2_score
y_true = [3.0, 5.0, 2.5, 7.0]
y_pred = [2.5, 5.0, 4.0, 8.0]
print(f"R2={r2_score(y_true, y_pred):.4f}") # R2=0.7241Exercise
Exercise
A model's squared errors sum to 40 on a test set. Predicting the test-set mean for every row would give squared errors that sum to 160. What is R squared, and what does it say about the model?
Show answer
R² = 1 - 40/160 = 0.75. The model removes 75% of the squared error that the mean baseline makes. It is clearly better than the baseline but not perfect.
Exercise
A colleague's model has an R squared of -0.2 on the test set. What does this mean, and what simple model beats it?
Show answer
The model's squared error is 20% larger than the baseline's (SSres = 1.2 × SStot). Predicting the mean of the test targets for every row would score 0 and so beats it.