Đại học Kinh tế Quốc dân – National Economics UniversityMICRO4ASIA
🏆 Arena
Sign inSign up
MICRO4ASIAThe six micro-credentials

Courses are delivered on the Limio learning platform. Limio

← Fundamental of Machine Learning

You are viewing a public preview

Sign in to save your progress, do assignments, join discussions and get feedback.

Sign in
Module 3: Model Evaluation and Optimization

3.5 Coefficient of Determination R²

Print / Save as PDF

3.5 Coefficient of Determination R²

After this unit you will be able to: explain what R² measures and read its value.

Key Points

  • R² = 1 − (model error) / (baseline error).
  • Numerator: the segments connecting the true values to the model's prediction line (solid boxes).
  • Denominator: the segments connecting the true values to the mean line — the baseline always predicts the mean value (dashed boxes).
  • A perfectly predicting model ⇒ numerator = 0 ⇒ R² = 1; a model no better than the baseline ⇒ numerator = denominator ⇒ R² = 0.

Study notes

By the end of this lesson you can:

  • Explain what question R-squared answers compared with MAE and MSE
  • Describe the baseline model that always predicts the mean
  • Compute R-squared = 1 - (model error) / (baseline error) for a small example
  • Interpret R-squared values of 1, 0, about 0.7 and below 0
  • Summarise the metrics covered in Modules 1 to 3

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.

A different question

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?

The lazy baseline

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.

The formula

R-squared = 1 - (model error) / (baseline error)

On the slide:

  • the numerator (solid boxes) comes from the segments joining each true value to the model's prediction line;
  • the denominator (dashed boxes) comes from the segments joining each true value to the mean line.

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.

Reading the value

R-squaredMeaning
1.0perfect: every prediction is exactly correct (numerator = 0)
about 0.7explains about 70% of the variation; often a solid, usable result
0.0no better than always guessing the average (numerator = denominator)
below 0worse 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.

Recap of the course content

The lecture ends the theory part with a recap:

  • Module 1: types of data and features, cleaning and preparing data, splitting into training, validation and test sets.
  • Module 2: Linear Regression, Logistic Regression, decision trees, Random Forest, SVM.
  • Module 3: accuracy, precision, recall and F1 for classification; MAE, MSE and R-squared for regression.

Next, everything is applied to real datasets in Python.

Worked example

Four houses, prices in billion VND: actual 2, 3, 5, 6. The model predicts 2.5, 3, 4.5, 6.

Step by step:

  1. Mean of the actual prices = (2 + 3 + 5 + 6) / 4 = 4. The baseline predicts 4 for every house.
  2. Baseline squared errors: (2-4)^2 + (3-4)^2 + (5-4)^2 + (6-4)^2 = 4 + 1 + 1 + 4 = 10.
  3. Model squared errors: (2-2.5)^2 + 0 + (5-4.5)^2 + 0 = 0.25 + 0.25 = 0.5.
  4. R-squared = 1 - 0.5 / 10 = 0.95: the model explains 95% of the variation in price.
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.

Common mistakes

  • Thinking R-squared cannot be negative. A model worse than the mean baseline gets a negative score.
  • Reading R-squared as accuracy. 0.7 does not mean "70% of predictions are correct"; it means 70% of the variation is explained.
  • Comparing R-squared with MAE directly. R-squared is unitless and relative; MAE is in the target's units. Report both.
  • Using one universal threshold for "good". About 0.7 is often good, but standards differ by field.

Summary

  • R-squared compares the model's error with the error of always predicting the mean.
  • Formula: 1 - (model squared errors) / (squared distances to the mean).
  • 1 = perfect, 0 = no better than the mean, negative = worse than the mean.
  • It is a normalised, relative score, unlike MAE and MSE, which are in raw units.
  • In scikit-learn: r2_score(y_true, y_pred).

Flashcards · 3.5 Coefficient of Determination R²

8 words
WordMeaningExample
  • R-squared (coefficient of determination)

    A score showing how much better a model is than always predicting the mean: 1 - model error / baseline error.

    r2_score(y_true, y_pred) -> 0.95

  • Mean baseline

    The laziest regression model, which always predicts the average of the target.

    Always predict 4 billion VND for every house.

  • R-squared = 1

    The model predicts every point exactly, so its error is zero.

    Numerator = 0 -> R-squared = 1.

  • R-squared = 0

    The model is no better than always predicting the mean.

    Numerator equals denominator.

  • Negative R-squared

    The model performs worse than simply predicting the mean every time.

    Reversed predictions give R-squared = -3.

  • Explained variation

    The share of the target's natural variation that the model accounts for.

    R-squared 0.7 -> explains about 70% of price variation.

  • Normalised metric

    A unitless score that can be compared across problems, unlike MAE or MSE.

    R-squared is unitless; MAE is in billion VND.

  • r2_score()

    The scikit-learn function that computes R-squared from true and predicted values.

    r2_score(actual, predicted)