Implement one gradient-descent step for logistic regression in NumPy

Quick Overview

Implement a single full-batch gradient-descent step for logistic regression in NumPy, updating the weights and the intercept from the mean binary log-loss. It tests vectorized gradient computation, evaluating both gradients at the same parameters, and care with array shapes and return types.

Implement one gradient-descent step for logistic regression in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `gd_step(X, y, w, b, lr)` from scratch (NumPy only, no sklearn) to perform exactly one full-batch gradient-descent step on the mean binary log-loss of logistic regression, returning `(w_new, b_new)`. `X` is `(n_samples, n_features)` with no ones column, `b` is a separate scalar intercept, and `y` is float `0.0`/`1.0` of shape `(n_samples,)`. The step must meet these requirements: - `p = 1 / (1 + np.exp(-(X @ w + b)))`. - `grad_w = X.T @ (p - y) / n_samples` and `grad_b = np.sum(p - y) / n_samples`, both at the incoming `w`, `b` (never use the updated `w` for `grad_b`). - Return `(w - lr * grad_w, b - lr * grad_b)`: `w_new` is an `np.ndarray` of shape `(n_features,)` and `b_new` is a `float`. - Exactly one step: no loop. ```hint Order of operations List every quantity that depends on the incoming parameters and compute all of them before you update anything. Then compare the shape and type of each returned value with the requirements. ``` ### Constraints & Assumptions - `X` has shape `(n_samples, n_features)` and holds only feature values; the intercept is the separate scalar `b`. - `y` has shape `(n_samples,)` and holds `0.0` or `1.0`. - `w` has shape `(n_features,)`, and `lr` is a scalar learning rate. - NumPy only: no scikit-learn or other ML libraries. ### Example ```python X = np.array([[1.0, 2.0], [3.0, 4.0]]) y = np.array([0.0, 1.0]) w = np.zeros(2); b = 0.0; lr = 0.1 w_new, b_new = gd_step(X, y, w, b, lr) # p = [0.5, 0.5], p - y = [0.5, -0.5] # grad_w = X.T @ [0.5, -0.5] / 2 = [-0.5, -0.5] -> w_new == [0.05, 0.05] # grad_b = 0.0 -> b_new == 0.0 ``` ### What a Strong Answer Covers - Probabilities for all samples from one vectorized expression in `X`, `w` and `b`. - Both gradients averaged over the samples and evaluated at the incoming parameters. - Return values with the required shape and type, without modifying the caller's `w`. - A short derivation or justification of the gradient formula from the log-loss. - The time and space cost of the step, and edge cases such as very large logits. ### Follow-up Questions - How would you extend this into a full training loop with convergence checks? - How would you add L2 regularization to the update? - Why can `np.exp` overflow here, and how do you guard against it?

Overview: Implement a single full-batch gradient-descent step for logistic regression in NumPy, updating the weights and the intercept from the mean binary log-loss. It tests vectorized gradient computation, evaluating both gradients at the same parameters, and care with array shapes and return types.

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

Implement gd_step(X, y, w, b, lr) from scratch (NumPy only, no sklearn) to perform exactly one full-batch gradient-descent step on the mean binary log-loss of logistic regression, returning (w_new, b_new). X is (n_samples, n_features) with no ones column, b is a separate scalar intercept, and y is float 0.0/1.0 of shape (n_samples,).

The step must meet these requirements:

  • p = 1 / (1 + np.exp(-(X @ w + b))) .
  • grad_w = X.T @ (p - y) / n_samples and grad_b = np.sum(p - y) / n_samples , both at the incoming w , b (never use the updated w for grad_b ).
  • Return (w - lr * grad_w, b - lr * grad_b) : w_new is an np.ndarray of shape (n_features,) and b_new is a float .
  • Exactly one step: no loop.

Constraints & Assumptions

  • X has shape (n_samples, n_features) and holds only feature values; the intercept is the separate scalar b .
  • y has shape (n_samples,) and holds 0.0 or 1.0 .
  • w has shape (n_features,) , and lr is a scalar learning rate.
  • NumPy only: no scikit-learn or other ML libraries.

Example

X = np.array([[1.0, 2.0], [3.0, 4.0]])
y = np.array([0.0, 1.0])
w = np.zeros(2); b = 0.0; lr = 0.1
w_new, b_new = gd_step(X, y, w, b, lr)
# p = [0.5, 0.5], p - y = [0.5, -0.5]
# grad_w = X.T @ [0.5, -0.5] / 2 = [-0.5, -0.5] -> w_new == [0.05, 0.05]
# grad_b = 0.0 -> b_new == 0.0

What a Strong Answer Covers Guidance

  • Probabilities for all samples from one vectorized expression in X , w and b .
  • Both gradients averaged over the samples and evaluated at the incoming parameters.
  • Return values with the required shape and type, without modifying the caller's w .
  • A short derivation or justification of the gradient formula from the log-loss.
  • The time and space cost of the step, and edge cases such as very large logits.

Follow-up Questions Guidance

  • How would you extend this into a full training loop with convergence checks?
  • How would you add L2 regularization to the update?
  • Why can np.exp overflow here, and how do you guard against it?