Two-Column predict_proba From Raw Logistic Regression Parameters

Quick Overview

Implement a two-column predict_proba for binary logistic regression from a given coefficient vector and intercept, returning class probabilities in ascending class order. Tests vectorized NumPy, a numerically stable sigmoid, and the floating-point care needed so every row sums to 1.

Two-Column predict_proba From Raw Logistic Regression Parameters

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `predict_proba_two_column(X, w, b)` for a binary logistic regression model whose parameters are given directly. `X` is a `float64` matrix of shape `(n_samples, n_features)`, `w` is a coefficient vector of shape `(n_features,)`, and `b` is a float intercept. Return scikit-learn's `predict_proba` layout: a `float64` array of shape `(n_samples, 2)` with the classes in ascending order. Requirements: - Column 1 is $P(y=1) = \sigma(Xw + b)$, the sigmoid of `X @ w + b`. - Column 0 is $P(y=0) = 1 - P(y=1)$. - Each row sums to exactly 1.0. - Use a numerically stable sigmoid (for example `scipy.special.expit`), not the naive `1 / (1 + np.exp(-z))`. - No model fitting: the parameters are given. Example: ```python X = np.array([[1.0, 2.0], [3.0, 4.0], [-1.0, 0.5], [0.0, 0.0]]) w = np.array([0.5, -1.0]) b = 0.25 out = predict_proba_two_column(X, w, b) assert out.shape == (4, 2) assert_allclose(out.sum(axis=1), 1.0) assert_allclose(out[:, 1], expit(X @ w + b)) ``` The logits here are `[-1.25, -2.25, -0.75, 0.25]`, so `out` is approximately `[[0.7773, 0.2227], [0.9047, 0.0953], [0.6792, 0.3208], [0.4378, 0.5622]]`. ```hint Row sums Decide which single quantity you need to compute, and how to get the other column from it so every row sums to 1.0 without a renormalization step. ``` ### Constraints & Assumptions - `X` is a 2-D `float64` array of shape `(n_samples, n_features)`. - `w` has shape `(n_features,)` and `b` is a scalar. - Logits can be large in magnitude, so the sigmoid must not overflow. - The output is a `float64` array of shape `(n_samples, 2)`. ### What a Strong Answer Covers - Computing the logits with one vectorized matrix-vector product plus the intercept. - A sigmoid that stays finite and warning-free for large positive and negative logits. - The negative class in column 0 and the positive class in column 1, matching ascending class order. - Building the two columns so each row sums to 1.0, with the required shape and dtype. - The time and space cost of the computation. ### Follow-up Questions - Why can the naive `1 / (1 + np.exp(-z))` be a problem, and how does `expit` avoid it? - How would you generalize this to multiclass `predict_proba`? - For a very large logit, column 0 becomes exactly 0.0. When does that matter, and how would you compute accurate log-probabilities instead?

Overview: Implement a two-column predict_proba for binary logistic regression from a given coefficient vector and intercept, returning class probabilities in ascending class order. Tests vectorized NumPy, a numerically stable sigmoid, and the floating-point care needed so every row sums to 1.

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

Implement predict_proba_two_column(X, w, b) for a binary logistic regression model whose parameters are given directly. X is a float64 matrix of shape (n_samples, n_features), w is a coefficient vector of shape (n_features,), and b is a float intercept. Return scikit-learn's predict_proba layout: a float64 array of shape (n_samples, 2) with the classes in ascending order.

Requirements:

  • Column 1 is P(y=1)=σ(Xw+b)P(y=1) = \sigma(Xw + b) , the sigmoid of X @ w + b .
  • Column 0 is P(y=0)=1−P(y=1)P(y=0) = 1 - P(y=1) .
  • Each row sums to exactly 1.0.
  • Use a numerically stable sigmoid (for example scipy.special.expit ), not the naive 1 / (1 + np.exp(-z)) .
  • No model fitting: the parameters are given.

Example:

X = np.array([[1.0, 2.0], [3.0, 4.0], [-1.0, 0.5], [0.0, 0.0]])
w = np.array([0.5, -1.0])
b = 0.25

out = predict_proba_two_column(X, w, b)
assert out.shape == (4, 2)
assert_allclose(out.sum(axis=1), 1.0)
assert_allclose(out[:, 1], expit(X @ w + b))

The logits here are [-1.25, -2.25, -0.75, 0.25], so out is approximately [[0.7773, 0.2227], [0.9047, 0.0953], [0.6792, 0.3208], [0.4378, 0.5622]].

Constraints & Assumptions

  • X is a 2-D float64 array of shape (n_samples, n_features) .
  • w has shape (n_features,) and b is a scalar.
  • Logits can be large in magnitude, so the sigmoid must not overflow.
  • The output is a float64 array of shape (n_samples, 2) .

What a Strong Answer Covers Guidance

  • Computing the logits with one vectorized matrix-vector product plus the intercept.
  • A sigmoid that stays finite and warning-free for large positive and negative logits.
  • The negative class in column 0 and the positive class in column 1, matching ascending class order.
  • Building the two columns so each row sums to 1.0, with the required shape and dtype.
  • The time and space cost of the computation.

Follow-up Questions Guidance

  • Why can the naive 1 / (1 + np.exp(-z)) be a problem, and how does expit avoid it?
  • How would you generalize this to multiclass predict_proba ?
  • For a very large logit, column 0 becomes exactly 0.0. When does that matter, and how would you compute accurate log-probabilities instead?