download

Your regression R² is negative. What does that mean?

Machine learning~10 minReviewed 29 Oct 2026Free · No sign-in

Question

A model predicts how many minutes it takes to pack an order. You test it on two weeks:

weekactual minutespredicted minutes
12, 4, 6, 83, 5, 5, 7
24, 5, 5, 65, 6, 4, 5

In both weeks every prediction is off by exactly 1 minute. Yet r2_score gives 0.8 for week 1 and −1.0 for week 2.

What does a negative R² mean? Compute both values by hand and explain why the same errors give such different scores.

Show hints (3)
  1. "THE FORMULA"

    R² = 1 − SS_res / SS_tot. SS_res is the sum of the model's squared errors. SS_tot is the sum of squared distances from the mean of the actual values.

  2. "A BASELINE INSIDE"

    SS_tot is the error of a model that always predicts the mean of these actual values. So R² compares your model with one constant guess.

  3. "WEEK 2"

    Week 2's mean is 5. Predicting 5 for every order gives errors 1, 0, 0 and 1, so SS_tot = 2. The model's SS_res is 4.

Show checked explanation

R² = 1 − SS_res / SS_tot, where SS_tot is the squared error of always predicting the mean of the actual values. A negative R² means the model's squared error is larger than that: it does worse than one constant guess. In week 2, SS_res = 4 and SS_tot = 2, so R² = −1.

"SOLUTION · PYTHON"
from sklearn.metrics import r2_score, root_mean_squared_error

weeks = {1: ([2, 4, 6, 8], [3, 5, 5, 7]), 2: ([4, 5, 5, 6], [5, 6, 4, 5])}

for week, (actual, predicted) in weeks.items():
    mean = sum(actual) / len(actual)
    ss_res = sum((a - p) ** 2 for a, p in zip(actual, predicted))
    ss_tot = sum((a - mean) ** 2 for a in actual)
    r2 = r2_score(actual, predicted)
    rmse = root_mean_squared_error(actual, predicted)
    print(f"week {week}: SS_res={ss_res} SS_tot={ss_tot:g} "
          f"by hand={1 - ss_res / ss_tot:g} r2_score={r2:g} RMSE={rmse:g}")

# every actual value is 5, so SS_tot = 0
print(r2_score([5, 5, 5], [5, 5, 5]), r2_score([5, 5, 5], [4, 5, 6]))
"OUTPUT"
week 1: SS_res=4 SS_tot=20 by hand=0.8 r2_score=0.8 RMSE=1
week 2: SS_res=4 SS_tot=2 by hand=-1 r2_score=-1 RMSE=1
1.0 0.0

Code run on CPython 3.12 with scikit-learn 1.9 against the examples above. Checked by an editor other than the author.

Walkthrough

R² compares your model with the simplest guess: the mean of the actual values, predicted for every row.

  • SS_res = Σ(actual − predicted)², the model's squared error.
  • SS_tot = Σ(actual − mean)², the squared error of always predicting the mean.
  • R² = 1 − SS_res / SS_tot.
weekmeanSS_resSS_totR²RMSE
1549 + 1 + 1 + 9 = 201 − 4/20 = 0.81
2541 + 0 + 0 + 1 = 21 − 4/2 = −11

Read R² against two fixed points. A perfect model has SS_res = 0, so R² = 1. Predicting the mean gives SS_res = SS_tot, so R² = 0. A model worse than the mean has SS_res > SS_tot, and R² goes below 0. There is no lower limit. Predicting 8 for every order in week 1 gives SS_res = 36 + 16 + 4 + 0 = 56, so R² = 1 − 56/20 = −1.8.

Both weeks have the same errors and the same RMSE of 1 minute. What changed is the spread of the actual values. In week 1 the orders vary a lot, so a 1-minute error is small next to that spread. In week 2 they barely vary, and always guessing 5 beats the model.

Report RMSE or MAE next to R². They stay in minutes and don't depend on the spread.

On a test set, a negative R² usually means a poor model or test data that differs from training data, such as a new warehouse. The mean in SS_tot is the test set's own mean, which the model never saw. Even a constant model that predicts the training mean gets R² below 0 on test, unless the two means match exactly.

Common mistakes

  • Writing R² as SS_res / SS_tot. That ratio is 0 for a perfect model. The "1 −" is what makes a perfect model score 1.
  • Reading R² = 0.8 as "80% of predictions are right". In week 1 no prediction is exactly right. It means the squared error is 80% smaller than the mean's.
  • Comparing R² between datasets. Week 1 and week 2 have the same 1-minute errors, but R² is 0.8 and −1.

Follow-up questions

  • "What does r2_score return when every actual value is the same?" SS_tot is 0, so the formula divides by zero. By default (force_finite=True) it returns 1.0 for a perfect prediction and 0.0 otherwise, as the last line of the code shows. With force_finite=False they become nan and -inf. A single row gives nan and a warning.
  • "Can R² be negative on the training data?" Not for linear regression with an intercept, fitted by least squares. Predicting the mean (all weights 0) is one of its options, so it can't do worse on its own training rows. Without an intercept it can. On new data, any model can.

What a strong answer covers

Writes R² as 1 − SS_res / SS_tot and reads a negative value as worse than predicting the mean. Shows that the same errors give different R² when the spread changes. Knows what r2_score does on a constant target.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from Inside the Machine Learning Interview (Peng Shao, 2023); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.