All problems
R-Squared Score
Problem
Compute the coefficient of determination R^2 = 1 - SS_res / SS_tot, where SS_res = sum((y_i - pred_i)^2) and SS_tot = sum((y_i - mean_y)^2).
If SS_tot is 0, return 1.0 when SS_res is also 0 and 0.0 otherwise. Empty input returns 0.0.
Hint
Compare the residual sum of squares to the variance of y_true.
Test cases
case 1
in: [[1,2,3],[1,2,3]]
expected: 1
case 2
in: [[1,2,3],[2,2,2]]
expected: 0
case 3
in: [[1,2,3],[3,2,1]]
expected: -3
case 4
in: [[5,5,5],[5,5,6]]
expected: 0
Starter codePython
def r2_score(y_true, y_pred):
# Your code here
pass4 test cases · run your code against them in the editor