Softmax of decision_function Equals predict_proba for Multinomial Logistic Regression
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `proba_from_decision(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Fit `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0)` on `X` (shape `(n, 2)`) and a 3-class `y`, then return the row-wise softmax of decision scores that you compute by hand.
The function must:
- Leave `multi_class` unset.
- Compute the scores as `X @ coef_.T + intercept_`, giving shape `(n, 3)`. Do not call `decision_function`.
- Use a numerically stable softmax that subtracts each row's maximum before exponentiating.
- Return a `float64` array of shape `(n, 3)` that matches `model.predict_proba(X)`.
Example of how the result is checked:
```python
proba = proba_from_decision(X, y)
model = LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6,
C=1.0, class_weight=None, random_state=0).fit(X, y)
assert proba.shape == (len(X), 3)
assert_allclose(proba, model.predict_proba(X), rtol=1e-5)
```
```hint Shapes first
`coef_` stores one row per class. Work out which side of the product it belongs on, and how `intercept_` broadcasts, so the scores come out with one column per class.
```
### Constraints & Assumptions
- `X` has exactly 2 features and `y` has exactly 3 distinct classes.
- The hyperparameters are pinned as listed above.
- The output is compared with `model.predict_proba(X)` using `assert_allclose(rtol=1e-5)`.
### What a Strong Answer Covers
- A fit with the pinned hyperparameters and `multi_class` left unset, with a reason this matters for the identity.
- Scores built with the correct shapes from `coef_` and `intercept_`, and the column order they follow.
- A row-wise stable softmax and why the shift leaves the probabilities unchanged.
- Why the softmax of the scores equals `predict_proba` for this model, and when that identity breaks.
### Follow-up Questions
- Why subtract the row max before `np.exp`?
- Would a binary fit behave differently?
- Would the softmax of the scores still match `predict_proba` for a one-vs-rest logistic model, where each class gets its own binary fit?
Overview: Fit a three-class scikit-learn logistic regression, compute its decision scores by hand from the coefficients and intercepts, and turn them into class probabilities with a stable softmax. Tests how multinomial logistic regression maps linear scores to probabilities and when that mapping changes.