Reproduce logistic regression predict_proba from decision_function with expit
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `proba_via_decision_function(X_train, y_train, X_test)`. Fit a binary scikit-learn `LogisticRegression` on the training data and return the positive-class probabilities for `X_test` by applying `scipy.special.expit` to `model.decision_function(X_test)`. Do not call `predict_proba` or `predict_log_proba`.
Requirements:
- Fit with exactly: `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`.
- Use `expit`, not `1 / (1 + exp(-z))` directly.
- Return a 1-D `np.ndarray` of shape `(n_test,)`, dtype `float64`, with element `i` equal to `expit(decision_function(X_test)[i])`.
Example:
```python
proba = proba_via_decision_function(X_train, y_train, X_test)
# proba.shape == (X_test.shape[0],), dtype float64
# np.allclose(proba, model.predict_proba(X_test)[:, 1]) == True
```
```hint Know the score
Check what `decision_function` returns for a binary model: its shape, and which entry of `model.classes_` a positive score points to.
```
### Constraints & Assumptions
- `X_train` has shape `(n_train, n_features)` and `X_test` has shape `(n_test, n_features)`, with the same features in the same order.
- `y_train` holds the labels 0 and 1, both present.
- NumPy, SciPy and scikit-learn are available.
### What a Strong Answer Covers
- Fitting the estimator with exactly the pinned hyperparameters.
- What `decision_function` computes for a binary model and how it relates to column 1 of `predict_proba`.
- Returning a 1-D `float64` array of length `n_test`.
- Why `expit` is the safer choice over a hand-written sigmoid, in terms of numerical behavior.
- How to confirm the result matches `predict_proba` without calling it inside the function.
### Follow-up Questions
- Why prefer `expit` over `1 / (1 + np.exp(-z))`?
- How would this generalize to the multiclass case?
- How would you compute log-probabilities from the same scores without losing precision?
Overview: Fit a binary scikit-learn logistic regression and rebuild its positive-class probabilities from decision_function scores with scipy's expit, without calling predict_proba. It tests what the decision function returns, how the sigmoid maps log-odds to probabilities, and numerically safe sigmoid computation.