Compute the Gradient of the L2-Penalized Logistic Regression Loss

Quick Overview

Write a vectorized NumPy function that returns the weight and intercept gradients of the L2-penalized logistic regression log-loss. It tests whether you can derive the log-loss gradient, apply the penalty derivative with the right constant, and leave the intercept out of the penalty.

Compute the Gradient of the L2-Penalized Logistic Regression Loss

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `l2_logistic_gradient(X, y, w, b, lam)` using only NumPy (no sklearn). It returns the gradient of ```text J(w, b) = -mean(y*log(p) + (1-y)*log(1-p)) + (lam/2) * np.sum(w**2) ``` with `p = sigmoid(X @ w + b)`. Compute it in a single vectorized evaluation (no loop over samples). The function must follow these rules: - Return `(grad_w, grad_b)` with `grad_w = X.T @ (p - y) / n_samples + lam * w`. - The penalty derivative is `lam * w`, not `2 * lam * w`. - `grad_b = np.sum(p - y) / n_samples` with no penalty term (the intercept is not penalized). - `grad_w` has shape `(n_features,)` and `grad_b` is a Python float. ```hint Two terms, two parameters Differentiate the data term and the penalty term separately, and check which of the two parameters each term actually depends on. ``` ### Constraints & Assumptions - `X` has shape `(n_samples, n_features)` with no ones column. The intercept is the separate scalar `b`. - `y` is a float array of `0.0`/`1.0` labels with shape `(n_samples,)`. - `w` has shape `(n_features,)`. ### What a Strong Answer Covers - The mean data-term gradient built from the residual `p - y` with matrix operations and no per-sample loop. - Why the L2 penalty contributes `lam * w` and why it applies to the weights only. - An unpenalized intercept gradient, with a reason for leaving the intercept out of the penalty. - Output shapes and types that match the contract. - The cost of the computation in terms of `n_samples` and `n_features`. ### Follow-up Questions - How would you extend this to L1 (Lasso) regularization? - Why compute `p` with a numerically stable sigmoid? - How would you confirm that the gradient you return is correct?

Overview: Write a vectorized NumPy function that returns the weight and intercept gradients of the L2-penalized logistic regression log-loss. It tests whether you can derive the log-loss gradient, apply the penalty derivative with the right constant, and leave the intercept out of the penalty.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement l2_logistic_gradient(X, y, w, b, lam) using only NumPy (no sklearn). It returns the gradient of

J(w, b) = -mean(y*log(p) + (1-y)*log(1-p)) + (lam/2) * np.sum(w**2)

with p = sigmoid(X @ w + b). Compute it in a single vectorized evaluation (no loop over samples).

The function must follow these rules:

  • Return (grad_w, grad_b) with grad_w = X.T @ (p - y) / n_samples + lam * w .
  • The penalty derivative is lam * w , not 2 * lam * w .
  • grad_b = np.sum(p - y) / n_samples with no penalty term (the intercept is not penalized).
  • grad_w has shape (n_features,) and grad_b is a Python float.

Constraints & Assumptions

  • X has shape (n_samples, n_features) with no ones column. The intercept is the separate scalar b .
  • y is a float array of 0.0 / 1.0 labels with shape (n_samples,) .
  • w has shape (n_features,) .

What a Strong Answer Covers Guidance

  • The mean data-term gradient built from the residual p - y with matrix operations and no per-sample loop.
  • Why the L2 penalty contributes lam * w and why it applies to the weights only.
  • An unpenalized intercept gradient, with a reason for leaving the intercept out of the penalty.
  • Output shapes and types that match the contract.
  • The cost of the computation in terms of n_samples and n_features .

Follow-up Questions Guidance

  • How would you extend this to L1 (Lasso) regularization?
  • Why compute p with a numerically stable sigmoid?
  • How would you confirm that the gradient you return is correct?