Implement greedy forward feature selection with cross-validation
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `forward_select(estimator, X, y, cv=5, scoring=None, k_features=None, tol=None)`. Starting from no features, each step adds the remaining column whose subset (selected + candidate) has the highest mean cross-validated score. Return `(selected_columns, scores)`: the columns in selection order and the mean CV score after each addition.
The function must follow these rules:
- `cv` is an int (unshuffled contiguous K folds, matching `KFold(cv)`), an object with `.split(X, y)`, or an iterable of `(train_idx, val_idx)`.
- Fit a fresh `clone(estimator)` per fold. Score with `est.score(X_val, y_val)`, or with `scoring(est, X_val, y_val)` if `scoring` is given.
- Ties go to the candidate earliest in `X.columns` order. Do not mutate `X` or `y`.
- Stop at `k_features` (default: all columns). With `tol` set, stop before adding a feature unless its gain over the previous step's score is strictly `> tol`. The first feature is always added.
- A `k_features` that is not an int in `[1, n_features]` raises `ValueError`.
- `X` is a `pandas.DataFrame`. Subset columns by name and rows with `.iloc`.
For example, suppose `est` produces these mean CV scores on the given folds:
```python
# mean CV scores: {b}=0.9, {a}=0.2, {c}=0.1, {b,c}=0.85, {a,b}=0.8, {a,b,c}=0.4
forward_select(est, X, y, cv=2) # (['b', 'c', 'a'], [0.9, 0.85, 0.4])
forward_select(est, X, y, cv=2, tol=0.0) # (['b'], [0.9])
```
Without `tol`, the search adds `b`, then `c` (0.85 beats 0.8), then `a`, even though the last step lowers the score. With `tol=0.0`, the second step would change the score by -0.05, which is not above 0, so the search stops after `b`.
```hint Comparing candidates fairly
Every candidate subset should be scored on exactly the same splits. Think about when the folds are built, and what happens if `cv` is a generator that can only be iterated once.
```
```hint Where the stopping check goes
The `tol` check compares the best candidate of the current step with the score recorded at the previous step. Work out whether it runs before or after you append, and what it does on the first step.
```
### What a Strong Answer Covers
- Building the folds once from all three accepted `cv` forms, with an integer `cv` matching unshuffled `KFold` exactly.
- Scoring each candidate subset with a fresh clone per fold and the right scoring function, averaged over the folds.
- Tie-breaking by original column order and the exact `k_features` and `tol` stopping rules, including the first step.
- Validating `k_features` and leaving `X` and `y` unmodified.
- The number of fits the search needs as a function of features, steps and folds.
### Follow-up Questions
- What is the computational cost and how would you reduce it?
- Why can the selected subset overfit the CV folds, and how would you get an honest estimate?
- How would you extend this to floating (SFFS) selection?
Overview: Implement greedy forward feature selection that adds one column at a time by mean cross-validated score, accepting several fold specifications, custom scorers and two stopping rules. It tests fair comparison of candidates on fixed folds, tie handling and an exact stopping condition.