Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #1717

R² Score

The R² score (coefficient of determination) measures what fraction of the target's total variance a regression model successfully explains — unlike MSE/MAE/RMSE, it's on a fixed, comparable scale rather than dependent on the target's raw units.

Formula

\[ R^2 = 1 - \frac{SS_{res}}{SS_{tot}}, \qquad SS_{res}=\sum(y_i-\hat{y}_i)^2, \qquad SS_{tot}=\sum(y_i-\bar{y})^2 \]

\(SS_{res}\) (residual sum of squares) is exactly the numerator behind MSE — the model's actual error. \(SS_{tot}\) (total sum of squares) is how much the target varies from its own mean — the error a "predict the mean for everyone" baseline would produce. R² compares the model's error against that naive baseline.

Worked Example

Using the same regression example: \(y=[52,58,62,68,75]\), \(\bar{y}=63\), \(SS_{res}=2.4\) (5 × MSE of 0.48).

\[ SS_{tot} = (52-63)^2+(58-63)^2+(62-63)^2+(68-63)^2+(75-63)^2 = 121+25+1+25+144 = 316 \] \[ R^2 = 1 - \frac{2.4}{316} = 1 - 0.0076 \approx \mathbf{0.9924} \]
from sklearn.metrics import r2_score

y_true = [52, 58, 62, 68, 75]
y_pred = [51.8, 57.4, 63.0, 68.6, 74.2]

print(r2_score(y_true, y_pred))   # 0.9924

Reading R² Values

R² ValueMeaning
1.0Perfect predictions — model explains 100% of the target's variance
0.0No better than always predicting the mean
NegativeWorse than always predicting the mean — a genuinely bad model

R² can go negative — a common source of confusion, since it superficially resembles a percentage. A negative R² means the model's actual predictions are worse than the naive "just guess the average every time" baseline.

Why R² Alone Isn't Always Enough

A high R² doesn't guarantee good predictions in absolute terms — it only measures relative improvement over the mean-baseline. A model could have R²=0.9 while still being off by a business-unacceptable amount on individual predictions, especially if the target has huge natural variance to begin with. Always pair R² with an absolute-error metric like RMSE or MAE for a complete picture.

Adjusted R² — Correcting for Feature Count

\[ \text{Adjusted } R^2 = 1-\left[\frac{(1-R^2)(n-1)}{n-p-1}\right] \]

\(p\) is the number of features. Plain R² can only increase (or stay flat) as more features are added, even useless ones — Adjusted R² penalizes this, decreasing if a new feature doesn't genuinely improve the fit beyond what chance alone would predict.

Practical Use Cases

  • Communicating "how much of the outcome does this model actually explain" in a scale-independent, easily comparable way
  • Comparing models across different datasets or targets, where raw MSE/RMSE values aren't directly comparable

Common Mistakes

  • Treating R² as a percentage of "accuracy" the way it's sometimes informally described — it specifically measures variance explained, not correctness in the way classification accuracy does.
  • Comparing plain R² across models with different numbers of features without switching to Adjusted R².
  • Reporting a suspiciously perfect R² (like 0.9924 here, or higher) without checking for data leakage on real-world data.

Interview Relevance

Q: "What does a negative R² mean, and how is that possible?" It means the model's predictions are worse, on average, than simply always predicting the target's mean value — R² isn't bounded below by 0 the way it might intuitively seem; a poorly fit or badly overfit model evaluated on new data can genuinely underperform this trivial baseline.

Practice Question

A model has \(SS_{res}=50\) and \(SS_{tot}=40\). Compute R² and interpret what this unusual result means about the model's quality.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →