Compare one-vs-rest and multinomial logistic regression probabilities in scikit-learn

Quick Overview

Fit a multinomial logistic regression and a one-vs-rest wrapper on the same 3-class data in scikit-learn and compare their predicted probabilities. It tests how each strategy normalizes class scores, why their decision boundaries differ, and when one-vs-rest is still the better choice.

Compare one-vs-rest and multinomial logistic regression probabilities in scikit-learn

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `ovr_vs_multinomial(X: np.ndarray, y: np.ndarray) -> dict` to contrast two multiclass strategies on one 3-class dataset. Fit (a) `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0)` directly, which gives a multinomial softmax model, and (b) `OneVsRestClassifier` wrapping the same pinned estimator, which gives one binary model per class. Requirements: - Never pass `multi_class` anywhere. - Return a dict with exactly the keys `'multinomial_proba'`, `'ovr_proba'`, `'n_ovr_estimators'` and `'ovr_sub_coef_shape'`. - `'multinomial_proba'` and `'ovr_proba'` are each model's `predict_proba(X)`, shape `(n, 3)`, with rows summing to 1 (`OneVsRestClassifier` normalizes the OvR rows). The two matrices differ. - `'n_ovr_estimators'` is `len(ovr.estimators_)` as an int, and `'ovr_sub_coef_shape'` is `ovr.estimators_[0].coef_.shape`. ```hint What scikit-learn infers Check which loss `LogisticRegression` uses with `solver='lbfgs'` when `y` has three classes and `multi_class` is left out. ``` ```hint Where the row sums come from Each binary sub-model returns its own sigmoid probability for its class. Look at what `OneVsRestClassifier.predict_proba` does with those three numbers in each row. ``` ### Constraints & Assumptions - `X` is a numeric `(n, d)` array and `y` holds exactly 3 distinct classes. - Fit both models on `X` as given with the exact pinned settings. Both probability arrays are compared against identically pinned reference fits with `assert_allclose(rtol=1e-5)`, the int and tuple keys with `==`, and every row sum is checked. ### What a Strong Answer Covers - Two estimators built with identical pins, `multi_class` never passed, and the multinomial loss inferred for `lbfgs` on three classes. - The four return values with the right types, including why there are 3 sub-estimators and why each has a `(1, d)` coefficient array. - Why both probability matrices have rows that sum to 1 and still differ. - How one joint softmax fit and three independent binary fits lead to different decision boundaries. ### Follow-up Questions - Why can OvR and multinomial produce different decision boundaries even on the same data? - When would you still prefer OvR despite the coupling advantage of multinomial? - What happens to this comparison if `y` has only two classes? - Which of the two probability matrices would you read as calibrated probabilities, and how would you check?

Overview: Fit a multinomial logistic regression and a one-vs-rest wrapper on the same 3-class data in scikit-learn and compare their predicted probabilities. It tests how each strategy normalizes class scores, why their decision boundaries differ, and when one-vs-rest is still the better choice.

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

Implement ovr_vs_multinomial(X: np.ndarray, y: np.ndarray) -> dict to contrast two multiclass strategies on one 3-class dataset. Fit (a) LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0) directly, which gives a multinomial softmax model, and (b) OneVsRestClassifier wrapping the same pinned estimator, which gives one binary model per class.

Requirements:

  • Never pass multi_class anywhere.
  • Return a dict with exactly the keys 'multinomial_proba' , 'ovr_proba' , 'n_ovr_estimators' and 'ovr_sub_coef_shape' .
  • 'multinomial_proba' and 'ovr_proba' are each model's predict_proba(X) , shape (n, 3) , with rows summing to 1 ( OneVsRestClassifier normalizes the OvR rows). The two matrices differ.
  • 'n_ovr_estimators' is len(ovr.estimators_) as an int, and 'ovr_sub_coef_shape' is ovr.estimators_[0].coef_.shape .

Constraints & Assumptions

  • X is a numeric (n, d) array and y holds exactly 3 distinct classes.
  • Fit both models on X as given with the exact pinned settings. Both probability arrays are compared against identically pinned reference fits with assert_allclose(rtol=1e-5) , the int and tuple keys with == , and every row sum is checked.

What a Strong Answer Covers Guidance

  • Two estimators built with identical pins, multi_class never passed, and the multinomial loss inferred for lbfgs on three classes.
  • The four return values with the right types, including why there are 3 sub-estimators and why each has a (1, d) coefficient array.
  • Why both probability matrices have rows that sum to 1 and still differ.
  • How one joint softmax fit and three independent binary fits lead to different decision boundaries.

Follow-up Questions Guidance

  • Why can OvR and multinomial produce different decision boundaries even on the same data?
  • When would you still prefer OvR despite the coupling advantage of multinomial?
  • What happens to this comparison if y has only two classes?
  • Which of the two probability matrices would you read as calibrated probabilities, and how would you check?