You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
After this unit you will be able to: explain what R² measures and read its value.
By the end of this lesson you can:
Key idea: R-squared tells you how much better your model is than the laziest possible model, one that always predicts the average; 1 means perfect, 0 means no better than the average, and negative means worse.
MAE and MSE answer "how big are the errors, in the target's units?". The coefficient of determination, usually called R-squared, answers a subtly different and in some ways more powerful question: how much better are my predictions than a naive baseline, such as always guessing the average value?
Imagine the laziest possible regression model. It ignores all input features and always predicts the average house price, whatever the house. On the slide this baseline is the horizontal mean line.
R-squared measures how much of the natural variation in house prices your model explains, compared with that "just guess the average" baseline.
R-squared = 1 - (model error) / (baseline error)
On the slide:
In the usual form, both are sums of squared errors: the model's squared errors in the numerator, and the squared distances from the mean in the denominator.
| R-squared | Meaning |
|---|---|
| 1.0 | perfect: every prediction is exactly correct (numerator = 0) |
| about 0.7 | explains about 70% of the variation; often a solid, usable result |
| 0.0 | no better than always guessing the average (numerator = denominator) |
| below 0 | worse than guessing the average |
R-squared usually ranges from 0 to 1, but it can go negative for a model that performs worse than the mean baseline. What counts as "good" depends on your field and problem.
R-squared is popular because it is normalised: not an absolute number in raw units like MAE or MSE, but a relative score that shows how much value your model adds on top of the simplest possible guess.
The lecture ends the theory part with a recap:
Next, everything is applied to real datasets in Python.
Four houses, prices in billion VND: actual 2, 3, 5, 6. The model predicts 2.5, 3, 4.5, 6.
Step by step:
import numpy as np
from sklearn.metrics import r2_score
actual = np.array([2, 3, 5, 6])
predicted = np.array([2.5, 3, 4.5, 6])
baseline = np.full(4, actual.mean())
print(r2_score(actual, predicted))
print(r2_score(actual, baseline))
print(r2_score(actual, np.array([6, 5, 3, 2])))
Expected output:
0.95
0.0
-3.0
The baseline itself scores exactly 0. The last model predicts the prices in reverse order; its squared errors sum to 16 + 4 + 4 + 16 = 40, so R-squared = 1 - 40/10 = -3, much worse than guessing the average.
r2_score(y_true, y_pred).Flashcards · 3.5 Coefficient of Determination R²
8 wordsA score showing how much better a model is than always predicting the mean: 1 - model error / baseline error.
The laziest regression model, which always predicts the average of the target.
The model predicts every point exactly, so its error is zero.
The model is no better than always predicting the mean.
The model performs worse than simply predicting the mean every time.
The share of the target's natural variation that the model accounts for.
A unitless score that can be compared across problems, unlike MAE or MSE.
The scikit-learn function that computes R-squared from true and predicted values.