Skip to content

R² — Coefficient of Determination

\[ R^2 = 1 - \frac{SS_{\text{res}}}{SS_{\text{tot}}} \quad \text{where} \quad SS_{\text{res}} = \sum_i (y_{\text{true},i} - y_{\text{pred},i})^2,\quad SS_{\text{tot}} = \sum_i (y_{\text{true},i} - \bar{y})^2 \]

Implementation: sklearn.metrics.r2_score.

Running example:

  1. Residual sum of squares: \(SS_{\text{res}} = 1 + 4 + 25 + 100 + 400 = 530\)
  2. Mean of true values: \(\bar{y} = (5 + 20 + 50 + 100 + 200) / 5 = 75\)
  3. Total sum of squares: \(SS_{\text{tot}} = 70^2 + 55^2 + 25^2 + 25^2 + 125^2 = 4900 + 3025 + 625 + 625 + 15625 = 24800\)
  4. Result: \(R^2 = 1 - 530 / 24800 \approx 0.979\)

Interpretation

R² measures the fraction of variance in the Target explained by the model. R² = 1 is perfect; R² = 0 means the model is no better than predicting the mean; R² < 0 means the model is worse than the mean. Values are only directly comparable across datasets with the same Target distribution.

When to use

  • As a normalised, scale-free accuracy measure that communicates "how much better than a naive baseline".
  • When comparing models across different Target scales or units.

Disadvantages

  • R² can be misleadingly high when the Target range is large; a model can score R² ≈ 0.99 while having substantial absolute errors.
  • Sensitive to outliers in the Target (they inflate \(SS_{\text{tot}}\), which in turn inflates R²).
  • Not appropriate on its own when the distribution of \(y_{\text{true}}\) spans several orders of magnitude — consider R²(log) instead.

Compute cost

Low — two passes over predictions.


← Back to Metrics index