All problems
Gradient Descent Step
Problem
Perform one gradient descent step for linear regression with intercept. With predictions pred_i = w0 + w1*x_i and residuals r_i = pred_i - y_i, the gradients are (2/n)*sum(r_i) for w0 and (2/n)*sum(r_i*x_i) for w1.
Return [w0 - lr*grad0, w1 - lr*grad1]; empty X returns [w0, w1].
Hint
Compute residuals once, then use them for both the intercept and slope gradients.
Test cases
case 1
in: [[1,2,3],[2,4,6],0,0,0.1]
expected: [0.8,1.8666666666666665]
case 2
in: [[1,2,3],[2,4,6],1,2,0.1]
expected: [0.8,1.6]
case 3
in: [[0],[1],1,0,0.5]
expected: [1,0]
case 4
in: [[],[],3,4,0.1]
expected: [3,4]
Starter codePython
def gd_step_linear(X, y, w0, w1, lr):
# Returns [new_w0, new_w1]
# Your code here
pass4 test cases · run your code against them in the editor