Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-134

L2 Gradient Step

Problem

Perform one gradient descent step for ridge regression without an intercept. The gradient is (2/n) * X^T (X w - y) + 2 * lam * w, and the new weights are w - lr * grad.

Return the updated weight list; empty X returns a copy of w.

Hint

The L2 penalty adds 2*lam*w to the ordinary least-squares gradient.

Test cases

case 1
in: [[[1],[2]],[2,4],[1],0.1,0.1]
expected: [1.48]
case 2
in: [[[1]],[1],[0],0.5,1]
expected: [1]
case 3
in: [[[1],[2]],[2,4],[2],0.1,0]
expected: [2]
case 4
in: [[],[],[1,2],0.1,0.1]
expected: [1,2]
Starter codePython
def l2_gradient_step(X, y, w, lr, lam):
    # Your code here
    pass

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