All problems
Leave-One-Out Cross-Validation R-Squared
Problem
Return the leave-one-out cross-validation R-squared for simple linear regression: 1 - SSE_loo/SST, where each point is predicted by a line fit on the other n-1 points and SST uses the overall mean. Return 0.0 when n < 3, the lengths differ, y is constant, or any leave-one-out x has zero variance.
Hint
Refit for every left-out point; the score can go negative.
Test cases
case 1
in: [[1,2,3],[2,4,6]]
expected: 1
case 2
in: [[1,2,3,4],[2,4,6,9]]
expected: 0.9330324029923496
case 3
in: [[1,2,3,4,5],[1,3,2,5,4]]
expected: 0.09426020408163249
case 4
in: [[1,2],[3,5]]
expected: 0
case 5
in: [[5,5,5],[1,2,3]]
expected: 0
Starter codePython
def leave_one_out_r2(x, y):
# Your code here
pass5 test cases · run your code against them in the editor