Reproducible Logistic Regression Probabilities with the saga Solver
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `saga_probabilities(X_train, y_train, X_test)`: fit a binary logistic regression with the `saga` solver and return the positive-class probabilities for `X_test`.
Requirements:
- Build `Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression(...))])`. The scaler is required for convergence within `max_iter`.
- Classifier: `LogisticRegression(penalty='l2', C=1.0, solver='saga', max_iter=20000, tol=1e-8, fit_intercept=True, random_state=0)`.
- Fit on `(X_train, y_train)` and return `pipe.predict_proba(X_test)[:, 1]` as a 1-D `np.ndarray` of shape `(n_test,)` and dtype `float64`.
- The result must match the deterministic `lbfgs` solution on the same standardized data, up to a tight numerical tolerance.
```hint Why two solvers can agree
Think about the shape of the L2-regularized logistic loss and how many minimizers it can have.
```
```hint Where the randomness enters
Work out what `saga` samples at each step, which argument controls it, and what has to happen before a stochastic method stops moving.
```
### Constraints & Assumptions
- `y_train` contains only the integer labels `0` and `1`.
- `X_train` and `X_test` are 2-D numeric arrays with the same columns.
- The probabilities are compared with those of an `lbfgs` fit of the same pipeline, so they must agree to a tight tolerance and are not expected to be bit-identical.
### What a Strong Answer Covers
- Builds the pipeline exactly as specified and returns the positive-class column as a 1-D float64 array.
- Explains why a stochastic solver and a deterministic one can reach the same coefficients on this problem.
- Explains why `saga` needs standardized features and what the tight `tol` and large `max_iter` are for.
- Names what `random_state` controls and how to confirm the run converged.
- Keeps the scaler fit on training data only.
### Follow-up Questions
- Why does `saga` need scaling when `lbfgs` tolerates unscaled data better?
- When would you actually prefer `saga` over `lbfgs`?
- If you dropped the scaler and gave `saga` unlimited iterations, would its probabilities match the scaled pipeline's? Why or why not?
- How would you detect that `saga` stopped before converging, and what would you change?
Overview: Fit a scikit-learn logistic regression with the stochastic saga solver on standardized features and return test-set probabilities that agree with the deterministic lbfgs solution. It tests convexity of the regularized objective, why saga needs feature scaling, and how to pin randomness and check convergence.