Root Mean Squared Error (RMSE) & R-Squared (R²)

Basics
BasicsRegression MetricsNumPyGoogleMetaAmazon

Problem

Implement rmse_and_r2(y_true, y_pred) returning (rmse, r2) where R2=1−∑(yi−y^i)2∑(yi−yˉ)2R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}.

Why It Matters: RMSE reports error in original target units while R2R^2 measures how much variance your model explains over a naive mean baseline.

Sample Inputs & Expected Outputs

Example 1 — Perfect fit

>>> rmse_and_r2([1, 2, 3], [1, 2, 3])
(0.0, 1.0)

Zero error gives RMSE=0.0 and R²=1.0.

Example 2 — Constant mean baseline

>>> rmse_and_r2([1, 2, 3], [2, 2, 2])
(0.8165, 0.0)

Predicting the mean ȳ=2 always yields R²=0.0.

Example 3 — Worse than mean (negative R²)

>>> rmse_and_r2([1, 2, 3], [4, 4, 4])
(2.1602, -6.0)

Predictions worse than ȳ produce a negative R².

Constraints & Edge Cases

  • Return (rmse, r2) as Python floats.
  • Assume y_true has non-zero variance.

Constraints & Evaluation Criteria

  • Return `(rmse, r2)` as Python floats.
  • Assume `y_true` has non-zero variance.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[1,2,3] vs [1,2,3]
Expected Output / Criteria
(0.0, 1.0)