Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-012

MSE Gradient

Problem

Compute the gradient of the mean squared error J(w) = (1/n) * sum((x_i . w - y_i)^2) with respect to the weight vector w.

grad = (2/n) * X^T (X w - y). X is a list of feature rows and w is a weight vector of the same dimension. Return the gradient as a list; empty X returns a zero vector of length len(w).

Hint

Compute residuals Xw - y first, then multiply residuals by the columns of X.

Test cases

case 1
in: [[[1,0],[0,1]],[1,1],[0,0]]
expected: [-1,-1]
case 2
in: [[[1],[2],[3]],[2,4,6],[1]]
expected: [-9.333333333333332]
case 3
in: [[[1],[2]],[2,4],[2]]
expected: [0]
case 4
in: [[[1,2],[3,4]],[1,2],[0.5,-0.5]]
expected: [-9,-13]
Starter codePython
def mse_gradient(X, y, w):
    # Returns the gradient as a list
    # Your code here
    pass

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