Elastic-Net Logistic Regression: Count Zero Coefficients Across an l1_ratio Sweep
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `elasticnet_l1_ratio_sweep(X, y, l1_ratios)`. `X` is float64 `(n_samples, n_features)`, `y` is `(n_samples,)` with labels in `{0, 1}`, and `l1_ratios` is an ascending 1-D float array with values in `[0, 1]`. For each value, fit a fresh binary `LogisticRegression` and return the count of (near) zero coefficients. The function returns only the counts, not the coefficients.
Requirements:
- Fit with exactly: `penalty='elasticnet'`, `l1_ratio=l1_ratio`, `C=0.1`, `solver='saga'`, `fit_intercept=True`, `max_iter=50000`, `tol=1e-8`, `random_state=0`.
- Element `i` is `np.sum(np.isclose(model.coef_.ravel(), 0.0, atol=1e-6))` for the model fit with `l1_ratios[i]`.
- Return dtype `np.int64`, shape `(len(l1_ratios),)`.
For example, with one standardized feature:
```python
X = np.array([[-1.0], [-1.0], [1.0], [1.0]])
y = np.array([0, 0, 1, 1])
l1_ratios = np.array([0.0, 0.1, 0.5, 1.0])
out = elasticnet_l1_ratio_sweep(X, y, l1_ratios)
# out == np.array([0, 0, 1, 1], dtype=np.int64)
```
```hint Fresh fits
Build the estimator inside the loop so nothing from the previous value carries over, and fix the output dtype up front.
```
```hint Reading the counts
Write down when a single coefficient is exactly zero at the optimum. How does that threshold depend on `l1_ratio` and `C`, and what else in the test moves when the optimum moves?
```
### Constraints & Assumptions
- `X` is already standardized: do not re-scale.
- `y` holds labels in `{0, 1}`.
- `l1_ratios` is ascending, 1-D, with values in `[0, 1]`; element `i` of the output belongs to `l1_ratios[i]`.
- Only `saga` supports elastic-net: do not change the solver.
### What a Strong Answer Covers
- Fits a new estimator for every value with all pinned arguments, including `fit_intercept=True` and `solver='saga'`.
- Counts zeros with the specified `np.isclose` tolerance and returns an `np.int64` array of shape `(len(l1_ratios),)` in input order.
- Explains why sparsity comes from the L1 share of the penalty and why `l1_ratio=0` gives no zeros in practice.
- Says whether the counts must be non-decreasing in `l1_ratio`, with the reason.
- Discusses the cost of the sweep and how to speed it up, including what a speed-up does to reproducibility.
### Follow-up Questions
- Why does `l1_ratio=0` typically yield zero exact zeros?
- How would you make the sweep faster across many `l1_ratios`?
- Can the count of zeros drop as `l1_ratio` rises at a fixed `C`? Describe the mechanism.
- For a production model, how would you choose `C` and `l1_ratio` instead of reading them off a sparsity curve?
Overview: Write a function that fits a fresh elastic-net logistic regression in scikit-learn for each value in an l1_ratio grid and returns how many coefficients are zero at each setting. It tests how the L1 share of the penalty drives sparsity at a fixed C and how the counts behave across the sweep.