All problems
Ridge Regression Closed Form
Problem
Fit a ridge regression with one feature and an intercept: minimize sum((y_i - a - b*x_i)^2) + alpha * b^2.
The closed form is b = Sxy / (Sxx + alpha) and a = mean_y - b * mean_x, where Sxy = sum((x_i - mean_x) * (y_i - mean_y)) and Sxx = sum((x_i - mean_x)^2). Return [a, b]; empty input returns [0.0, 0.0].
Hint
Ridge adds alpha to the slope denominator only; the intercept still passes through (mean_x, mean_y) minus the penalty.
Test cases
case 1
in: [[1,2,3,4,5],[3,5,7,9,11],1]
expected: [1.5454545454545459,1.8181818181818181]
case 2
in: [[1,2,3,4,5],[3,5,7,9,11],0]
expected: [1,2]
case 3
in: [[1,2,3],[2,4,6],3]
expected: [2.4,0.8]
case 4
in: [[2,2,2],[3,4,5],1]
expected: [4,0]
Starter codePython
def ridge_regression_fit(X, y, alpha):
# Returns [intercept, slope]
# Your code here
pass4 test cases · run your code against them in the editor