R-squared Score

Easy
~15 min
code completion

R² (Coefficient of Determination)

MSE tells you how large the errors are, but not how well you're doing relative to a baseline. answers: "How much better is my model than just predicting the mean?"

where:

  • — residual sum of squares
  • — total sum of squares
  • Interpretation:

  • R² = 1.0: perfect predictions
  • R² = 0.0: no better than predicting the mean
  • R² < 0: worse than predicting the mean
  • Your task:

    Implement r_squared(y_true, y_pred).

    Example Tests

    Perfect predictions: R2 = 1.0

    Input: {"y_pred":[1,2,3,4,5],"y_true":[1,2,3,4,5]}

    Expected: 1

    Predicting the mean: R2 = 0.0

    Input: {"y_pred":[6,6,6,6,6],"y_true":[2,4,6,8,10]}

    Expected: 0

    Good but not perfect predictions

    Input: {"y_pred":[1.5,2,2.5],"y_true":[1,2,3]}

    Expected: 0.75

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.