Vectorized Logistic Regression Gradient with the Intercept in the Design Matrix

Quick Overview

Write a vectorized NumPy function that returns the mean log-loss gradient of logistic regression when the intercept is already a column of ones in the design matrix. It tests deriving the gradient, handling array shapes, and understanding why the bias term needs no special case.

Vectorized Logistic Regression Gradient with the Intercept in the Design Matrix

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `logistic_gradient_design(X, y, w)` from scratch (no sklearn) with the intercept absorbed as `w[0]`. `X` already has a leading column of ones, so do not prepend another one. Return the gradient of the mean logistic loss (binary cross-entropy averaged over samples) as an array of shape `(d,)`. ```python def logistic_gradient_design(X: np.ndarray, y: np.ndarray, w: np.ndarray) -> np.ndarray: ... ``` Requirements: - Inputs: `X` has shape `(n_samples, d)`, `y` holds 0.0/1.0 floats with shape `(n_samples,)`, and `w` has shape `(d,)`. - Compute `p = 1 / (1 + np.exp(-(X @ w)))`. - Return `X.T @ (p - y) / n_samples` as an `np.ndarray` of shape `(d,)`. - No regularization, no explicit loop, no sklearn. For example: ```python X = np.array([[1.0, 2.0], [1.0, -1.0]]) y = np.array([1.0, 0.0]) w = np.array([0.0, 0.0]) # p = [0.5, 0.5], so p - y = [-0.5, 0.5] # grad = X.T @ (p - y) / 2 = [0.0, -0.75] ``` ```hint Start from one sample Differentiate the log-loss of a single sample with respect to its logit first, then use the chain rule to stack all samples into one matrix product. ``` ### What a Strong Answer Covers - The derivation from mean binary cross-entropy to `X.T @ (p - y) / n`, including the derivative of the sigmoid. - One vectorized expression with the right shapes, and no extra column of ones. - Why the intercept needs no special case when its column is part of `X`. - Practical checks: the sign of the result, shape mismatches that broadcast silently, and sigmoid overflow for large logits. ### Follow-up Questions - How would you add L2 regularization without penalizing the intercept? - Why can the intercept share the same formula here? - How would you check numerically that the gradient is correct?

Overview: Write a vectorized NumPy function that returns the mean log-loss gradient of logistic regression when the intercept is already a column of ones in the design matrix. It tests deriving the gradient, handling array shapes, and understanding why the bias term needs no special case.

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

Implement logistic_gradient_design(X, y, w) from scratch (no sklearn) with the intercept absorbed as w[0]. X already has a leading column of ones, so do not prepend another one. Return the gradient of the mean logistic loss (binary cross-entropy averaged over samples) as an array of shape (d,).

def logistic_gradient_design(X: np.ndarray, y: np.ndarray, w: np.ndarray) -> np.ndarray:
    ...

Requirements:

  • Inputs: X has shape (n_samples, d) , y holds 0.0/1.0 floats with shape (n_samples,) , and w has shape (d,) .
  • Compute p = 1 / (1 + np.exp(-(X @ w))) .
  • Return X.T @ (p - y) / n_samples as an np.ndarray of shape (d,) .
  • No regularization, no explicit loop, no sklearn.

For example:

X = np.array([[1.0, 2.0],
              [1.0, -1.0]])
y = np.array([1.0, 0.0])
w = np.array([0.0, 0.0])
# p = [0.5, 0.5], so p - y = [-0.5, 0.5]
# grad = X.T @ (p - y) / 2 = [0.0, -0.75]

What a Strong Answer Covers Guidance

  • The derivation from mean binary cross-entropy to X.T @ (p - y) / n , including the derivative of the sigmoid.
  • One vectorized expression with the right shapes, and no extra column of ones.
  • Why the intercept needs no special case when its column is part of X .
  • Practical checks: the sign of the result, shape mismatches that broadcast silently, and sigmoid overflow for large logits.

Follow-up Questions Guidance

  • How would you add L2 regularization without penalizing the intercept?
  • Why can the intercept share the same formula here?
  • How would you check numerically that the gradient is correct?