Fit a multinomial (softmax) logistic regression in scikit-learn and return predict_proba

Quick Overview

Fit a three-class multinomial logistic regression with pinned scikit-learn settings and return predicted class probabilities for new points. It tests how the softmax model differs from one-vs-rest, which solver settings make a fit reproducible, and how predict_proba follows from the fitted coefficients.

Fit a multinomial (softmax) logistic regression in scikit-learn and return predict_proba

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `multinomial_proba(X, y, X_new)`: fit a multinomial (softmax) logistic regression on `X` (float, shape `(n, 2)`) and `y` (1-D int labels, classes `0/1/2`), and return `predict_proba(X_new)`. Column `j` is the probability of `classes_[j]`. Requirements: - Pin exactly `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, penalty='l2', class_weight=None, random_state=0)`. - Do not pass `multi_class`. It is deprecated since scikit-learn 1.5 and raises a `FutureWarning`; `lbfgs` with three or more classes already fits the multinomial loss. - Return a `float64` array of shape `(X_new.shape[0], 3)` with each row summing to `1.0`. - Fit on `X` and `y` as given, with no scaling or preprocessing. Example: ```python X = np.array([[0.,0.],[1.,1.],[2.,2.],[3.,3.],[4.,4.],[5.,5.]]) y = np.array([0, 0, 1, 1, 2, 2]) X_new = np.array([[1.5, 1.5]]) out = multinomial_proba(X, y, X_new) # out.shape == (1, 3); out.sum(axis=1) == [1.0] ``` ```hint Defaults first Before adding any argument beyond the pinned ones, check which multiclass loss `lbfgs` already uses by default when there are three classes. ``` ### Constraints & Assumptions - `X` is a float array of shape `(n, 2)`; `y` holds integer labels from `{0, 1, 2}`, and all three classes appear. - The output is graded with `assert_allclose(rtol=1e-5)` against an identically pinned reference fit, plus a row-sum check. ### What a Strong Answer Covers - Builds the estimator with exactly the pinned arguments and leaves out the deprecated `multi_class`. - Explains what the multinomial model computes and why its rows sum to 1. - Returns a `float64` array of shape `(n_new, 3)` whose columns follow `classes_`. - Says which settings make the result reproducible against the reference, and which pinned ones `lbfgs` ignores. - Names the edge cases that would break the output contract. ### Follow-up Questions - How does softmax (multinomial) loss differ from one-vs-rest here? - Why pin `random_state` and `tol` for a deterministic grader? - How would you reproduce `predict_proba` by hand from `coef_` and `intercept_`?

Overview: Fit a three-class multinomial logistic regression with pinned scikit-learn settings and return predicted class probabilities for new points. It tests how the softmax model differs from one-vs-rest, which solver settings make a fit reproducible, and how predict_proba follows from the fitted coefficients.

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

Implement multinomial_proba(X, y, X_new): fit a multinomial (softmax) logistic regression on X (float, shape (n, 2)) and y (1-D int labels, classes 0/1/2), and return predict_proba(X_new). Column j is the probability of classes_[j].

Requirements:

  • Pin exactly LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, penalty='l2', class_weight=None, random_state=0) .
  • Do not pass multi_class . It is deprecated since scikit-learn 1.5 and raises a FutureWarning ; lbfgs with three or more classes already fits the multinomial loss.
  • Return a float64 array of shape (X_new.shape[0], 3) with each row summing to 1.0 .
  • Fit on X and y as given, with no scaling or preprocessing.

Example:

X = np.array([[0.,0.],[1.,1.],[2.,2.],[3.,3.],[4.,4.],[5.,5.]])
y = np.array([0, 0, 1, 1, 2, 2])
X_new = np.array([[1.5, 1.5]])
out = multinomial_proba(X, y, X_new)
# out.shape == (1, 3); out.sum(axis=1) == [1.0]

Constraints & Assumptions

  • X is a float array of shape (n, 2) ; y holds integer labels from {0, 1, 2} , and all three classes appear.
  • The output is graded with assert_allclose(rtol=1e-5) against an identically pinned reference fit, plus a row-sum check.

What a Strong Answer Covers Guidance

  • Builds the estimator with exactly the pinned arguments and leaves out the deprecated multi_class .
  • Explains what the multinomial model computes and why its rows sum to 1.
  • Returns a float64 array of shape (n_new, 3) whose columns follow classes_ .
  • Says which settings make the result reproducible against the reference, and which pinned ones lbfgs ignores.
  • Names the edge cases that would break the output contract.

Follow-up Questions Guidance

  • How does softmax (multinomial) loss differ from one-vs-rest here?
  • Why pin random_state and tol for a deterministic grader?
  • How would you reproduce predict_proba by hand from coef_ and intercept_ ?