Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Linear models

Logistic regression with an L2 penalty

Medium · Target 20 minutes · Python and NumPy

Train p = σ(Xw + b) by batch gradient descent on

L = −(1/n) Σ [yi log pi + (1 − yi) log(1 − pi)] + (lam/2)‖w‖²,

starting from w = 0, b = 0, for steps iterations with learning rate lr. The bias is not penalised. Compute both gradients from the current parameters, then update both. Return [w₁, …, w_d, b]. Logits in the tests can exceed 700 in magnitude.

Examples

Input X = [[1], [-1]], y = [1, 0], lr = 1, steps = 1, lam = 0

Output [0.5, 0]

Both predictions start at ½. One step moves w to 0.5 and leaves b at 0.

Input X = [[0, 0], [1, 1], [2, 2], [3, 3]], y = [0, 0, 1, 1], lr = 0.5, steps = 500, lam = 0.1

Output [1.0112, 1.0112, -3.0334]

Separable data. The penalty keeps the weights finite.

Input X = [[1000], [-1000]], y = [1, 0], lr = 0.002, steps = 3, lam = 0

Output [1, 0]

After one step the logits are ±1000. A naive sigmoid overflows here.

Constraints

  • 1 ≤ n ≤ 1000, 1 ≤ d ≤ 20
  • 0 ≤ steps ≤ 2000
  • lam ≥ 0
  • Checked to a relative tolerance of 10⁻⁷

Hints

Hint 1

The derivative of the per-example loss with respect to the logit zᵢ is pᵢ − yᵢ. Start from there.

Hint 2

∂L/∂w = (1/n)Xᵀ(p − y) + lam·w and ∂L/∂b = mean(p − y). The penalty does not touch b.

Hint 3

For the sigmoid, compute 1/(1 + e^(−z)) where z ≥ 0 and e^z/(1 + e^z) where z < 0, so the exponential never sees a large positive number.

Where candidates go wrong

  • Penalising the bias. The tests do not, and it biases every prediction towards one half.
  • A sigmoid written as 1/(1 + np.exp(−z)): with z = −1000 the exponential overflows to inf. The answer, 0, is right, but NumPy warns and some setups treat warnings as errors.
  • Writing the gradient as Xᵀ(y − p) and then adding it. One sign error and the loss climbs.
  • Forgetting the 1/n on the data term but keeping lam·w unscaled, or the other way round.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

This is the most common "implement a model" question in ML interviews. It tests the gradient of cross-entropy, whether the bias is penalised, and whether your sigmoid survives a logit of −1000. The derivation is usually asked before any code.

The theory behind it is in the lesson Linear and logistic regression from scratch.


Keep going