Implement the Logistic Regression Forward Pass in NumPy

Quick Overview

Implement the forward pass of a binary logistic regression model in NumPy. Given a feature matrix, weights and an intercept, compute the logits, apply the sigmoid and return one positive-class probability per sample. Tests vectorized linear algebra, output shapes and numerically stable sigmoid code.

Implement the Logistic Regression Forward Pass in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `predict_proba_from_params(X, w, b)`, the forward pass of an already trained binary logistic regression model. You are given a float64 matrix `X` of shape `(n_samples, n_features)`, a float64 weight vector `w` of shape `(n_features,)` and a float intercept `b`. Return the positive-class probability for every sample: $$P(y=1 \mid x_i) = \sigma(x_i^\top w + b), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}$$ Requirements: - Compute the logits `z = X @ w + b`, then apply the sigmoid elementwise. - Return a flat float64 `np.ndarray` of shape `(n_samples,)` that holds only the positive-class probability. - Every returned value must lie strictly in (0, 1). No training is involved: `w` and `b` are given. Example: ```python X = np.array([[0.0, 10.0, 2.0]]) w = np.array([2.0, -1.0, 5.0]) b = 1.0 # z = 0*2 + 10*(-1) + 2*5 + 1 = 1.0 predict_proba_from_params(X, w, b) # -> array([0.7310585786300049]) ``` ```hint Watch the exponent Work out what `np.exp(-z)` returns when a logit is around -1000 and what your formula outputs after that. Then check a logit around +50 against the strict (0, 1) requirement. ``` ### Constraints & Assumptions - `X` is 2-D with shape `(n_samples, n_features)`, `w` has shape `(n_features,)` and `b` is a scalar. - All inputs are finite float64 values. - NumPy is available. The function should handle the whole batch at once. ### What a Strong Answer Covers - Computing all logits with one matrix-vector product and broadcasting the intercept. - Returning exactly the shape and dtype asked for, `(n_samples,)` float64, including when there is a single sample. - How the sigmoid behaves in float64 for large positive and large negative logits, and how the implementation avoids overflow. - How the strict (0, 1) requirement is met when rounding produces exactly 0 or 1. - Time and space complexity in terms of the number of samples and features. ### Follow-up Questions - How would you avoid overflow in the sigmoid for large-magnitude z? - How would you extend the function to return both class probabilities, and how would you keep P(y=0) accurate when P(y=1) is very close to 1? - If these probabilities feed a log-loss computation, why might you compute the loss directly from the logits?

Overview: Implement the forward pass of a binary logistic regression model in NumPy. Given a feature matrix, weights and an intercept, compute the logits, apply the sigmoid and return one positive-class probability per sample. Tests vectorized linear algebra, output shapes and numerically stable sigmoid code.

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

Implement predict_proba_from_params(X, w, b), the forward pass of an already trained binary logistic regression model. You are given a float64 matrix X of shape (n_samples, n_features), a float64 weight vector w of shape (n_features,) and a float intercept b. Return the positive-class probability for every sample:

P(y=1∣xi)=σ(xi⊤w+b),σ(z)=11+e−zP(y=1 \mid x_i) = \sigma(x_i^\top w + b), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}

Requirements:

  • Compute the logits z = X @ w + b , then apply the sigmoid elementwise.
  • Return a flat float64 np.ndarray of shape (n_samples,) that holds only the positive-class probability.
  • Every returned value must lie strictly in (0, 1).

No training is involved: w and b are given.

Example:

X = np.array([[0.0, 10.0, 2.0]])
w = np.array([2.0, -1.0, 5.0])
b = 1.0
# z = 0*2 + 10*(-1) + 2*5 + 1 = 1.0
predict_proba_from_params(X, w, b)
# -> array([0.7310585786300049])

Constraints & Assumptions

  • X is 2-D with shape (n_samples, n_features) , w has shape (n_features,) and b is a scalar.
  • All inputs are finite float64 values.
  • NumPy is available. The function should handle the whole batch at once.

What a Strong Answer Covers Guidance

  • Computing all logits with one matrix-vector product and broadcasting the intercept.
  • Returning exactly the shape and dtype asked for, (n_samples,) float64, including when there is a single sample.
  • How the sigmoid behaves in float64 for large positive and large negative logits, and how the implementation avoids overflow.
  • How the strict (0, 1) requirement is met when rounding produces exactly 0 or 1.
  • Time and space complexity in terms of the number of samples and features.

Follow-up Questions Guidance

  • How would you avoid overflow in the sigmoid for large-magnitude z?
  • How would you extend the function to return both class probabilities, and how would you keep P(y=0) accurate when P(y=1) is very close to 1?
  • If these probabilities feed a log-loss computation, why might you compute the loss directly from the logits?