Logistic Regression Coefficient Growth Under Perfect Separation
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `separation_coef_growth(X, y, Cs)`. `X` (`(n_samples, 1)` float64) and `y` (labels in `{0, 1}`) are perfectly separable by a threshold on the single feature. For each `C` in the ascending 1-D array `Cs`, fit a fresh `LogisticRegression` and record the magnitude of the feature coefficient.
Requirements:
- Fit each model with exactly `penalty='l2'`, `C=C`, `solver='lbfgs'`, `fit_intercept=True`, `max_iter=200000`, `tol=1e-10`.
- Return a float64 `np.ndarray` of shape `(len(Cs),)`.
- Element `i` equals `abs(float(model.coef_.ravel()[0]))` for the fit at `Cs[i]` (no intercept, no sign).
For example:
```python
X = np.array([[-2.], [-1.], [1.], [2.]])
y = np.array([0, 0, 1, 1])
Cs = np.array([0.01, 1.0, 100.0])
out = separation_coef_growth(X, y, Cs)
# out is shape (3,), strictly increasing:
# larger C -> weaker penalty -> larger |coef_|
```
```hint What C scales
Write down the penalized objective that `LogisticRegression` minimizes and note which term `C` multiplies. Then ask what the unpenalized log-likelihood does on separable data as the coefficient grows.
```
```hint Why the tight settings
Consider how flat the objective is along the coefficient direction when the data are separable, and what an optimizer that stops early would report.
```
### Constraints & Assumptions
- `X` has shape `(n_samples, 1)` and dtype float64. `y` holds labels in `{0, 1}`, with both classes present.
- The data are perfectly separable by a threshold on the single feature.
- `Cs` is a 1-D array of positive values in ascending order.
- The output is a float64 array of shape `(len(Cs),)`.
### What a Strong Answer Covers
- A fresh `LogisticRegression` per `C` with exactly the required hyperparameters, and the absolute coefficient stored in a float64 array of the right shape
- Why the maximum likelihood estimate does not exist under perfect separation and how the L2 penalty keeps the coefficient finite
- Why the magnitude increases with `C`, and what the large `max_iter` and tiny `tol` protect against
- The cost of the loop over `Cs` and inputs that would break it
### Follow-up Questions
- Why does the returned array increase with `C`?
- How could you detect separation before fitting?
- What are safer alternatives to a growing L2 coefficient under separation?
Overview: Fit L2-regularized logistic regression across a range of C values on perfectly separable one-feature data and record how the coefficient magnitude changes. It checks understanding of why the maximum likelihood estimate does not exist under separation and how the penalty keeps coefficients finite.