Anime ninja owl choosing between a stack of data cards and a flexible curved ruler.

Why Is My R² Score Negative on the Test Set?

Hey friends! Imagine predicting delivery times for three orders. You correctly predict which will arrive first, second and third. Then you score the predictions and get an R² of −0.75. How can a score with a square in its name fall below zero?

R² compares two sums of squared errors. A negative score means that, on the rows you scored, your predictions accumulated more squared error than a single guess equal to the average actual outcome. If you’re building your first model, you can work through this example alongside the Free 90-Day ML Roadmap.

Three deliveries and a surprisingly good guess

The deliveries took 8, 10 and 12 minutes. Your model predicted 11, 12 and 13 minutes. It overshot every delivery, by 3, 2 and 1 minutes respectively. Square those misses and add them up. The model’s total is 9 + 4 + 1 = 14.

Now imagine guessing 10 minutes for every order. Ten is the mean of the three actual times. You miss the first and last by two minutes each and get the middle one exactly right. Your squared errors add up to 4 + 0 + 4 = 8.

R² = 1 − (model squared error / squared error from guessing the mean). Here that gives 1 − 14/8 = −0.75. In everyday terms, the model accumulated 75% more squared error than the mean guess. It still ranked all three deliveries correctly. Ranking alone won’t tell a customer when their lunch will arrive.

Reproduce the result in Python

actual = [8, 10, 12]
predicted = [11, 12, 13]

mean_actual = sum(actual) / len(actual)
model_error = sum((y - p) ** 2 for y, p in zip(actual, predicted))
mean_error = sum((y - mean_actual) ** 2 for y in actual)

print(1 - model_error / mean_error)

This prints −0.75. Try replacing the predictions with three tens. Both errors are now 8, giving R² = 0. Then try the exact delivery times. The numerator drops to zero and R² rises to 1. These checks assume the actual times vary. Identical actual times make the denominator zero. See how scikit-learn handles this edge case in its r2_score documentation.

Which average could you actually have predicted?

There’s a wrinkle when you evaluate on a test set. The 10-minute guess uses the average of deliveries that have already happened. When you’re quoting a time for a new order, you only have past deliveries. Suppose their average was 6 minutes. Predicting 6 for each test order gives squared errors of 4, 16 and 36, for a total of 56. Your model’s 14 beats that historical baseline even though its test R² is negative.

For your first machine learning project, record both comparisons. Report R² on the held-out rows, then score a baseline you could have used at prediction time on those same rows. For deliveries, a starting point might be the average time from the training period.

What to inspect before changing the model

Put actual and predicted values side by side, keeping the order ID with each pair. In our example, every prediction is late and the largest miss is on the quickest order. On your own validation data, check whether that pattern repeats across days or comes from a handful of unusual orders. A second metric can make the size of the error easier to discuss. Here the mean absolute error is (3 + 2 + 1)/3 = 2 minutes.

If your project predicts future orders, validate on later deliveries and train on earlier ones. The blog’s guide to when cross-validation can mislead you goes deeper into choosing a split. Keep a separate final test period for the model you settle on.

Subscribe to our awesome newsletter to get the best content on your journey to learn Machine Learning, including some exclusive free goodies!

HOW IS MACHINE LEARNING