Boruta Feature Selection with Shadow Features
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement four functions for Boruta feature selection: building shadow features, counting hits against the best shadow, a binomial decision rule, and the full selection loop. `boruta_select(X, y, n_iter=20, alpha=0.05, n_estimators=100, seed=0)` returns `(status, hits)`, both int arrays of length `p`.
- `make_shadow_features(X, rng)`: return `np.hstack([X, shadow])`, where `shadow[:, j] = X[rng.permutation(n), j]` (a fresh permutation per column, in column order).
- `count_hits(importances, n_real)`: the first `n_real` entries are real features and the rest are shadows. Return a bool mask over the real features, `True` where the importance is strictly greater than the max shadow importance. Ties are not hits.
- `boruta_decision(hits, n_iter, alpha)`: under $X \sim \text{Binom}(n_{iter}, 0.5)$, return `1` if $P(X \ge \text{hits}) \le \alpha$, else `-1` if $P(X \le \text{hits}) \le \alpha$, else `0`. No multiple-testing correction.
- `boruta_select`: use `rng = np.random.default_rng(seed)`. On iteration `it`, stop early if no feature is tentative. Otherwise shadow only the non-rejected features (confirmed features stay in), fit `RandomForestClassifier(n_estimators=n_estimators, random_state=seed + it)`, and add the hits to those features. After each iteration, apply `boruta_decision(hits, it + 1, alpha)` and update the status of currently tentative features only. Decided features never change.
Example:
```python
count_hits([0.3, 0.1, 0.05, 0.2, 0.08, 0.02], 3) # [True, False, False]
boruta_decision([10, 0, 5, 8, 9, 2, 1], 10, alpha=0.05)
# -> [1, -1, 0, 0, 1, 0, -1]
```
In the first call the best shadow importance is 0.2, and only the first real feature beats it. In the second, 9 hits out of 10 give $P(X \ge 9) = 11/1024 \approx 0.011$, so that feature is confirmed, while 8 hits give $P(X \ge 8) = 56/1024 \approx 0.055$, just above 0.05, so it stays tentative. The low counts mirror this: 1 hit is rejected and 2 hits stay tentative.
```hint Check the tail at the boundary
`binom.sf(k, n, p)` returns $P(X > k)$. Decide which `k` makes the tail include `hits` itself, and test it on the 8-hit and 9-hit cases above.
```
```hint Keep the indices aligned
Each iteration shadows only the non-rejected columns, so the hit mask from `count_hits` is indexed by those columns. Map it back to the original feature positions before adding.
```
### Constraints & Assumptions
- `X` has shape `(n, p)` with numeric values, and `y` holds class labels of length `n`.
- Status codes: `1` confirmed, `-1` rejected, `0` tentative. Every `hits[j]` lies in `[0, n_iter]`.
- `scipy.stats.binom` and scikit-learn are available.
### What a Strong Answer Covers
- Shadow columns built with one fresh permutation per column, drawn in column order from the shared generator.
- A hit rule with a strict comparison against the max shadow importance, and both binomial tails computed correctly at the boundary.
- A loop that shadows only non-rejected features, maps hits back to the original columns, seeds each forest with `seed + it`, counts `it + 1` trials, and stops once nothing is tentative.
- Decisions that stay frozen once made, plus why a small `n_iter` leaves every feature tentative.
- The cost of each iteration and what dominates it.
### Follow-up Questions
- Why is an uncorrected test risky with many features, and how would you fix it?
- Why compare against the max shadow importance, and when might you use a percentile instead?
- What biases does impurity-based `feature_importances_` introduce here?
Overview: Implement Boruta feature selection with permuted shadow features, a hit rule against the best shadow importance, and a one-sided binomial test that confirms or rejects features over repeated random forest fits. It tests permutation baselines, hypothesis testing on hit counts and careful state tracking across iterations.