Compare L2 logistic regression coefficients on raw vs standardized features in scikit-learn
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `def scaled_vs_unscaled_coefs(X, y) -> tuple`. Given `X` (`(n_samples, n_features)`, float64, columns on very different scales) and `y` in `{0, 1}`, fit the same L2 logistic regression on raw and standardized features and return `(coef_raw, coef_scaled)`.
Requirements:
- Fit (a) on raw `X` and (b) on `StandardScaler().fit_transform(X)`.
- Both fits use exactly: `penalty='l2'`, `C=0.01`, `solver='lbfgs'`, `fit_intercept=True`, `max_iter=5000`, `tol=1e-10`.
- Return a tuple of two float64 `np.ndarray`s of shape `(n_features,)`, each `model.coef_.ravel()` (no intercept).
Use `sklearn.linear_model.LogisticRegression` and `sklearn.preprocessing.StandardScaler`.
```hint Units and the penalty
Ask what happens to a feature's fitted coefficient when you record that feature in units 1000 times smaller, and what that does to its squared-coefficient penalty.
```
### What a Strong Answer Covers
- Two independent estimators with the exact hyperparameters, with the scaler applied only to the second fit.
- The shape and dtype of the return values: what `coef_` looks like for a binary target and why the intercept is left out.
- Why the two coefficient vectors differ, in terms of how a feature's units set the size of its coefficient and its share of the penalty.
- What can go wrong at fit time, such as convergence on badly scaled raw features under the fixed `max_iter` and `tol`.
### Follow-up Questions
- Why is L2 not scale-invariant while the unpenalized MLE is?
- Would fitting inside a `Pipeline` change anything?
- How would you convert `coef_scaled` back to raw units, and would the result then match `coef_raw`?
Overview: Fit the same L2-regularized logistic regression in scikit-learn on raw and standardized features and return both coefficient vectors. It tests how feature units interact with the L2 penalty, how scaling affects regularized fits, and how to compare coefficients across scalings.