Newton-Raphson Logistic Regression Fit From Scratch

Quick Overview

Implement binary logistic regression from scratch in NumPy with a fixed number of full Newton-Raphson (IRLS) steps. Tests the gradient and Hessian of the log-loss, array shapes and broadcasting, solving linear systems safely, and what goes wrong on separable or collinear data.

Newton-Raphson Logistic Regression Fit From Scratch

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `fit_logistic_newton(X, y, n_iters)` from scratch (no sklearn or statsmodels): a full Newton-Raphson (IRLS) training loop for binary logistic regression. `X` has shape `(n_samples, d)` and its first column is already all ones, so do not prepend an intercept column; the intercept is `w[0]`. `y` holds float labels 0.0 or 1.0 with shape `(n_samples,)`. Return `w`, an `np.ndarray` of shape `(d,)`. Requirements: - Initialize `w = np.zeros(d)`. - Run exactly `n_iters` iterations, with no tolerance-based early stop. - Each iteration takes a full Newton step as written below: no damping, no learning rate, no regularization. - Solve the Newton system with `np.linalg.solve`, not `np.linalg.inv`. Each iteration computes: ```python p = 1 / (1 + np.exp(-(X @ w))) g = X.T @ (p - y) / n_samples s = p * (1 - p) H = (X.T * s) @ X / n_samples w = w - np.linalg.solve(H, g) ``` ```hint Check the shapes Write down the shape of every quantity before coding: `p`, `s` and `y` have length `n_samples`, `g` has length `d`, and `H` is `d` by `d`. If `H` comes out `n_samples` by `n_samples`, a transpose is in the wrong place. ``` ```hint Skip the diagonal matrix `X.T * s` broadcasts the length-`n_samples` vector `s` across the columns of `X.T`, which weights each sample without building an `n_samples` by `n_samples` diagonal matrix. ``` ### Constraints & Assumptions - `X` is a float array of shape `(n_samples, d)` whose first column is all ones. - `y` is a float array of 0.0 and 1.0 values with shape `(n_samples,)`. - `n_iters` is a non-negative integer, and the loop runs exactly that many times. - Use NumPy only; sklearn and statsmodels are not allowed. ### What a Strong Answer Covers - A loop that starts at zero, runs exactly `n_iters` full Newton steps, and returns a `(d,)` array without adding an intercept column. - The gradient and Hessian of the mean negative log-likelihood, with shapes that line up and the weighted Hessian built by broadcasting. - Why the system is solved with `np.linalg.solve`, and the cost of one iteration. - The inputs that break the method: separable data, collinear columns, and overflow in `np.exp`. ### Follow-up Questions - Why prefer `np.linalg.solve` over `np.linalg.inv`? - What happens on perfectly separable data, and how would you guard against it? - Why is this loop equivalent to iteratively reweighted least squares (IRLS)? - When would you drop Newton's method for a first-order or quasi-Newton optimizer?

Overview: Implement binary logistic regression from scratch in NumPy with a fixed number of full Newton-Raphson (IRLS) steps. Tests the gradient and Hessian of the log-loss, array shapes and broadcasting, solving linear systems safely, and what goes wrong on separable or collinear data.

General logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement fit_logistic_newton(X, y, n_iters) from scratch (no sklearn or statsmodels): a full Newton-Raphson (IRLS) training loop for binary logistic regression. X has shape (n_samples, d) and its first column is already all ones, so do not prepend an intercept column; the intercept is w[0]. y holds float labels 0.0 or 1.0 with shape (n_samples,). Return w, an np.ndarray of shape (d,).

Requirements:

  • Initialize w = np.zeros(d) .
  • Run exactly n_iters iterations, with no tolerance-based early stop.
  • Each iteration takes a full Newton step as written below: no damping, no learning rate, no regularization.
  • Solve the Newton system with np.linalg.solve , not np.linalg.inv .

Each iteration computes:

p = 1 / (1 + np.exp(-(X @ w)))
g = X.T @ (p - y) / n_samples
s = p * (1 - p)
H = (X.T * s) @ X / n_samples
w = w - np.linalg.solve(H, g)

Constraints & Assumptions

  • X is a float array of shape (n_samples, d) whose first column is all ones.
  • y is a float array of 0.0 and 1.0 values with shape (n_samples,) .
  • n_iters is a non-negative integer, and the loop runs exactly that many times.
  • Use NumPy only; sklearn and statsmodels are not allowed.

What a Strong Answer Covers Guidance

  • A loop that starts at zero, runs exactly n_iters full Newton steps, and returns a (d,) array without adding an intercept column.
  • The gradient and Hessian of the mean negative log-likelihood, with shapes that line up and the weighted Hessian built by broadcasting.
  • Why the system is solved with np.linalg.solve , and the cost of one iteration.
  • The inputs that break the method: separable data, collinear columns, and overflow in np.exp .

Follow-up Questions Guidance

  • Why prefer np.linalg.solve over np.linalg.inv ?
  • What happens on perfectly separable data, and how would you guard against it?
  • Why is this loop equivalent to iteratively reweighted least squares (IRLS)?
  • When would you drop Newton's method for a first-order or quasi-Newton optimizer?