Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-125

Ridge Regression Solve

Problem

Solve the ridge regression normal equations for design matrix A (n x 2) and response b (length n):

(A^T A + lam*I) x = A^T b

Return the coefficients [x0, x1]. lam = 0 reduces to ordinary least squares.

Hint

The ridge penalty adds lam to the diagonal of the Gram matrix A^T A.

Test cases

case 1
in: [[[1,1],[1,2],[1,3]],[1,2,3],0]
expected: [0,1]
case 2
in: [[[1,1],[1,2],[1,3]],[1,2,3],1]
expected: [0.25,0.8333333333333334]
case 3
in: [[[1,0],[0,1]],[3,4],0]
expected: [3,4]
case 4
in: [[[1,0],[1,0]],[1,3],1]
expected: [1.3333333333333333,0]
Starter codePython
def ridge_regression(A, b, lam):
    # Your code here
    pass

4 test cases · run your code against them in the editor