Gradient of the Least-Squares Loss
~15 mincode completion
Implement ls_gradient(X, y, w) returning as an array of shape (d,), where n is the number of rows of X.
Examples
A perfect fit has zero residual and therefore zero gradient
- Input
- ls_gradient([[1, 0], [0, 1]], [1, 2], [1, 2])
- Output
- [0, 0]
Weights of zero leave the residual equal to minus the targets
- Input
- ls_gradient([[1, 0], [0, 1]], [1, 2], [0, 0])
- Output
- [-1, -2]
A single feature over three rows
- Input
- ls_gradient([[1], [2], [3]], [2, 4, 6], [1])
- Output
- [-9.33333]
Hints
Hint 1
Use a matrix product rather than nested loops, and check which operand transposes.
Hint 2
A common slip here: uses X instead of X.T and gets a gradient shaped like the data.
Requirements
X: design matrix, shape (n, d)y: targets, shape (n,): weights, shape (d,)
Return array of shape (d,)
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Try similar problems(4)
Where this shows up
~15 min
••••••••••••
6 employers weight this skill
4 quant funds, 1 big tech firm, 1 frontier lab. Top match scores 92.
Python
import numpy as np
def ls_gradient(X, y, w):
"""
Gradient of the mean squared error of a linear model.
Args:
X: design matrix, shape (n, d)
y: targets, shape (n,)
w: weights, shape (d,)
Returns:
array of shape (d,)
"""
# YOUR CODE HERE
pass