Implement stability selection with a coordinate-descent Lasso on random subsamples
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement a Lasso solver and a stability selector with NumPy and pandas. `lasso_coordinate_descent(X, y, alpha)` fits the Lasso by coordinate descent. `StabilitySelector` fits that Lasso on `n_resamples` random subsamples of a pandas DataFrame, records how often each feature gets a nonzero coefficient, and keeps the features whose frequency meets `threshold`.
```python
def lasso_coordinate_descent(X, y, alpha, max_iter=10000, tol=1e-10): ...
class StabilitySelector:
def __init__(self, alpha=0.1, n_resamples=50, sample_fraction=0.5,
threshold=0.6, seed=0): ...
def fit(self, X, y): ... # returns self
def transform(self, X): ...
```
Requirements:
- The Lasso minimizes $\frac{1}{2n}\lVert y - Xb \rVert_2^2 + \alpha \lVert b \rVert_1$ with no intercept, the same objective as scikit-learn's `Lasso(fit_intercept=False)`. All-zero columns get coefficient 0. Write the solver yourself with NumPy; scikit-learn is used only as a test oracle.
- `fit` creates `rng = np.random.default_rng(seed)` once. For each resample it draws `idx = rng.choice(n, size=floor(sample_fraction * n), replace=False)`, standardizes `X` on that subsample (population standard deviation; columns with zero standard deviation become all zeros), and centers `y` on the subsample.
- A feature counts as selected in a resample when `|coef| > 1e-8`.
- `fit` stores `selection_frequencies_`, a float64 `Series` named `"frequency"` indexed by the column names in column order, and `selected_features_`, a list of the columns whose frequency is `>= threshold`, in column order. It returns `self`.
- `transform(X)` returns a copy of `X` with only the selected columns. No method mutates its inputs.
- `fit` raises `ValueError` if `sample_fraction` or `threshold` is outside $(0, 1]$, if `n_resamples < 1`, or if the subsample size is 0.
Example:
```python
# Columns a..e are independent standard normal features, n = 200,
# and y = 3*a - 2*b + small noise.
sel = StabilitySelector(alpha=1.0, n_resamples=30, seed=0).fit(X, y)
sel.selection_frequencies_.tolist() # [1.0, 1.0, 0.0, 0.0, 0.0]
sel.selected_features_ # ['a', 'b']
```
```hint One coordinate at a time
With every coefficient but one held fixed, the objective is a one-dimensional quadratic plus an absolute value, which has a closed-form minimizer. Keep the residual `y - Xb` current so each update needs one pass over a single column.
```
```hint Reproducible frequencies
The same seed must give the same frequencies on every run. Check where the generator is created and in what order it is consumed.
```
### Constraints & Assumptions
- `X` is a pandas DataFrame of numeric columns with `n` rows and `p` columns, and `y` is an array or Series of length `n`, aligned with `X` by position.
- Some columns may be constant or entirely zero.
- NumPy and pandas only; scikit-learn may appear only in tests.
### What a Strong Answer Covers
- A correct coordinate-descent Lasso: the soft-threshold update, a residual kept current, zero columns skipped and a stopping rule
- Subsamples drawn without replacement from one seeded generator, with standardization and centering done on each subsample
- Frequencies and selected features stored with the required type, name and column order, with an inclusive threshold
- Input validation and no mutation of `X` or `y`
- The cost of the procedure and the reason selection frequencies are more stable than a single Lasso fit
### Follow-up Questions
- How would you choose `alpha` and `threshold` with control over the number of false selections?
- How would you make this fast for large `p` or many resamples?
- Why use complementary pairs or the randomized Lasso instead of plain subsampling?
Overview: Implement a coordinate-descent Lasso in NumPy and use it for stability selection: refit on many random subsamples of a DataFrame, track how often each feature is chosen, and keep the stable ones. It tests the soft-threshold update, per-subsample standardization, reproducible sampling and error control.