Reproduce One-vs-Rest Logistic Regression Probabilities by Hand

Quick Overview

Reproduce scikit-learn's one-vs-rest logistic regression probabilities by hand for a three-class target: fit one binary model per class, stack the positive-class probabilities and normalize each row. Tests how one-vs-rest works internally and how it differs from multinomial softmax regression.

Reproduce One-vs-Rest Logistic Regression Probabilities by Hand

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `manual_ovr_proba(X: np.ndarray, y: np.ndarray) -> np.ndarray` for a 3-class `y`. It must reproduce, by hand, the output of ```python OneVsRestClassifier( LogisticRegression(solver="lbfgs", max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0) ).fit(X, y).predict_proba(X) ``` without calling `OneVsRestClassifier`. Return a row-normalized `(n, 3)` `float64` ndarray. Requirements: - Iterate over the classes from `np.unique(y)` in ascending order, with binary target `(y == c).astype(int)`. - Fit a fresh `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0)` per class. - Stack each model's `predict_proba(X)[:, 1]` as the columns of an `(n, 3)` matrix. - Divide each row by its own sum so rows sum to 1. - The result is graded with `assert_allclose(rtol=1e-5)` against the real `OneVsRestClassifier` output. ```hint Match the configuration Work out what has to be identical between your per-class models and the ones the wrapper fits internally. Then check which of the pinned hyperparameters the lbfgs solver actually reads. ``` ```hint Look at the row sums Before normalizing, add up each row of the stacked matrix and ask why the sums are not 1. ``` ### Constraints & Assumptions - `y` has exactly 3 distinct classes. - `X` is a numeric matrix of shape `(n, d)`. - scikit-learn's `LogisticRegression` may be used. `OneVsRestClassifier` may not be called inside the function. ### What a Strong Answer Covers - Building one binary target per class in the same ascending class order the wrapper uses for its columns. - Fitting an independent, identically configured model per class and taking its positive-class probability. - Why the stacked probabilities do not sum to 1 and what the row normalization does. - What makes the manual result match the wrapper numerically, and which inputs could break the match. - The cost of the approach in terms of the number of classes and the cost of one fit. ### Follow-up Questions - How does this differ from multinomial (softmax) logistic regression? - What happens if a row's sum is 0 before normalization? - How would you generalize the function to K classes, and does anything change when K is 2?

Overview: Reproduce scikit-learn's one-vs-rest logistic regression probabilities by hand for a three-class target: fit one binary model per class, stack the positive-class probabilities and normalize each row. Tests how one-vs-rest works internally and how it differs from multinomial softmax regression.

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

Implement manual_ovr_proba(X: np.ndarray, y: np.ndarray) -> np.ndarray for a 3-class y. It must reproduce, by hand, the output of

OneVsRestClassifier(
    LogisticRegression(solver="lbfgs", max_iter=1000, tol=1e-6, C=1.0,
                       class_weight=None, random_state=0)
).fit(X, y).predict_proba(X)

without calling OneVsRestClassifier. Return a row-normalized (n, 3) float64 ndarray.

Requirements:

  • Iterate over the classes from np.unique(y) in ascending order, with binary target (y == c).astype(int) .
  • Fit a fresh LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0) per class.
  • Stack each model's predict_proba(X)[:, 1] as the columns of an (n, 3) matrix.
  • Divide each row by its own sum so rows sum to 1.
  • The result is graded with assert_allclose(rtol=1e-5) against the real OneVsRestClassifier output.

Constraints & Assumptions

  • y has exactly 3 distinct classes.
  • X is a numeric matrix of shape (n, d) .
  • scikit-learn's LogisticRegression may be used. OneVsRestClassifier may not be called inside the function.

What a Strong Answer Covers Guidance

  • Building one binary target per class in the same ascending class order the wrapper uses for its columns.
  • Fitting an independent, identically configured model per class and taking its positive-class probability.
  • Why the stacked probabilities do not sum to 1 and what the row normalization does.
  • What makes the manual result match the wrapper numerically, and which inputs could break the match.
  • The cost of the approach in terms of the number of classes and the cost of one fit.

Follow-up Questions Guidance

  • How does this differ from multinomial (softmax) logistic regression?
  • What happens if a row's sum is 0 before normalization?
  • How would you generalize the function to K classes, and does anything change when K is 2?