Implement the ridge regression loss and gradient with an unpenalized intercept

Quick Overview

Implement a NumPy function that returns the ridge regression loss and its gradients with respect to the weights and an unpenalized intercept. It tests careful differentiation of a squared-error objective with an L2 penalty, the scaling constants on each term, and shape-safe vectorized code.

Implement the ridge regression loss and gradient with an unpenalized intercept

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `ridge_loss_and_grad(X, y, w, b, lam)` that returns `(loss, grad_w, grad_b)` for the ridge regression objective $$L(w, b) = \frac{1}{2n}\lVert Xw + b - y\rVert_2^2 + \lambda \lVert w\rVert_2^2$$ where $n$ is the number of rows of `X`, the scalar $b$ is added to every prediction, and $\lambda$ is `lam`. The intercept `b` is not penalized. For example: ```text Input: X = [[1, 2], [3, 4]], y = [1, 2], w = [0.5, -0.5], b = 1.0, lam = 0.1 Output: (0.675, array([-2.4, -3.6]), -1.0) ``` ```hint Watch the constants Differentiate the data term and the penalty separately. In the data term, the 2 from the square meets a $\frac{1}{2}$ in front. Check whether the penalty has a matching $\frac{1}{2}$. ``` ### Constraints & Assumptions - `X` has shape `(n, p)`, `y` has length `n`, `w` has length `p`, and `b` and `lam` are scalars with `lam >= 0`. - Inputs may be Python lists or NumPy arrays. - `loss` and `grad_b` are returned as scalar floats, and `grad_w` as an ndarray of shape `(p,)`. ### What a Strong Answer Covers - Computing the residual $Xw + b - y$ once and reusing it for the loss and both gradients. - The correct scaling on each gradient term, including what the penalty contributes to `grad_w`. - Keeping `b` out of the penalty and deriving its gradient. - Vectorized NumPy code that accepts lists, avoids shape bugs such as broadcasting `(n,)` against `(n, 1)`, and returns the specified types and shapes. ### Follow-up Questions - How would you check these gradients numerically, and what agreement would you expect? - What value of `alpha` in scikit-learn's `Ridge` gives the same minimizer as this objective? - With `b` unpenalized, what is the closed-form minimizer of this loss? - Why is the intercept usually left out of the penalty, and what changes if you penalize it?

Overview: Implement a NumPy function that returns the ridge regression loss and its gradients with respect to the weights and an unpenalized intercept. It tests careful differentiation of a squared-error objective with an L2 penalty, the scaling constants on each term, and shape-safe vectorized code.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement ridge_loss_and_grad(X, y, w, b, lam) that returns (loss, grad_w, grad_b) for the ridge regression objective

L(w,b)=12n∥Xw+b−y∥22+λ∥w∥22L(w, b) = \frac{1}{2n}\lVert Xw + b - y\rVert_2^2 + \lambda \lVert w\rVert_2^2

where nn is the number of rows of X, the scalar bb is added to every prediction, and λ\lambda is lam. The intercept b is not penalized.

For example:

Input: X = [[1, 2], [3, 4]], y = [1, 2], w = [0.5, -0.5], b = 1.0, lam = 0.1
Output: (0.675, array([-2.4, -3.6]), -1.0)

Constraints & Assumptions

  • X has shape (n, p) , y has length n , w has length p , and b and lam are scalars with lam >= 0 .
  • Inputs may be Python lists or NumPy arrays.
  • loss and grad_b are returned as scalar floats, and grad_w as an ndarray of shape (p,) .

What a Strong Answer Covers Guidance

  • Computing the residual Xw+b−yXw + b - y once and reusing it for the loss and both gradients.
  • The correct scaling on each gradient term, including what the penalty contributes to grad_w .
  • Keeping b out of the penalty and deriving its gradient.
  • Vectorized NumPy code that accepts lists, avoids shape bugs such as broadcasting (n,) against (n, 1) , and returns the specified types and shapes.

Follow-up Questions Guidance

  • How would you check these gradients numerically, and what agreement would you expect?
  • What value of alpha in scikit-learn's Ridge gives the same minimizer as this objective?
  • With b unpenalized, what is the closed-form minimizer of this loss?
  • Why is the intercept usually left out of the penalty, and what changes if you penalize it?