L1 Logistic Regression Feature Selection with LogisticRegressionCV
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement the function below. `X` is an already-standardized float64 array of shape `(n_samples, n_features)` and `y` has labels in `{0, 1}`. Fit an L1-penalized `LogisticRegressionCV` and return the indices of the features whose refitted coefficients are non-zero.
```python
def logregcv_l1_selected_features(X: np.ndarray, y: np.ndarray) -> np.ndarray:
...
```
Requirements:
- Use exactly `Cs=[0.01, 0.1, 1.0, 10.0]` (an explicit list), `cv=StratifiedKFold(n_splits=3, shuffle=False)`, `scoring='accuracy'`, `penalty='l1'` and `solver='liblinear'`.
- Also set `refit=True`, `fit_intercept=True`, `max_iter=100000`, `tol=1e-10` and `random_state=0`.
- Select features with `np.flatnonzero(~np.isclose(model.coef_.ravel(), 0.0, atol=1e-8))`.
- Return indices (not coefficients or a boolean mask) as an `np.int64` array of shape `(k,)`, sorted ascending.
- Do not re-standardize `X`.
For example, on a synthetic dataset of shape `(200, 20)` where only features 3, 7 and 11 drive the label, the expected result is:
```python
out = logregcv_l1_selected_features(X, y)
out # array([3, 7, 11])
out.dtype # dtype('int64')
```
The exact set depends on the data and on the `C` that cross-validation picks, so a run on other data can keep a noise feature or drop a weak predictive one.
```hint Shape of coef_
For a binary problem `coef_` is a 2-D array with one row. Flatten it before turning it into positions.
```
```hint Indices and dtype
A boolean mask and an index array are different return types. Check which NumPy call turns a mask into ascending positions, and what integer type it returns on every platform.
```
### Constraints & Assumptions
- `X` is float64 with shape `(n_samples, n_features)` and is already standardized; the example uses 200 rows and 20 features.
- `y` is binary with labels 0 and 1, and each class has at least 3 rows so the stratified 3-fold split works.
- The output may be empty when the chosen `C` zeroes every coefficient.
### What a Strong Answer Covers
- A `LogisticRegressionCV` built with every required setting, and coefficients read from the refitted model.
- How cross-validation picks `C` from the grid, and what happens on a tie in mean accuracy.
- Turning near-zero coefficients into an ascending `np.int64` index array, including the empty case.
- Why the selected set is data-dependent: accuracy as a CV score, correlated features, and the liblinear intercept.
### Follow-up Questions
- Why standardize before applying an L1 penalty?
- How does the `Cs` grid interact with the number of selected features?
- Why use `np.isclose(..., atol=1e-8)` instead of an exact `!= 0` test?
- How would you check whether the selected feature set is stable?
Overview: Fit an L1-penalized logistic regression with scikit-learn's LogisticRegressionCV on standardized data and return the indices of features with non-zero coefficients. It tests cross-validated regularization, how the C grid controls sparsity, and careful handling of near-zero coefficients.