Random Forest Out-of-Bag Score With Uncovered Samples
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given per-tree predictions `tree_predictions` of shape `(n_trees, n_samples)` and each tree's `bootstrap_indices`, implement `oob_mask`, `oob_predict` and `oob_score` for a random forest. Each sample's prediction must use only the trees whose bootstrap excluded it.
- `oob_mask(bootstrap_indices, n_samples)` returns a boolean array of shape `(n_trees, n_samples)` that is `True` where the sample is absent from that tree's bootstrap.
- `oob_predict(tree_predictions, bootstrap_indices, task="classification")` returns a float array with one value per sample. Classification uses a hard majority vote, with ties going to the lowest label. Regression uses the mean. A sample with no OOB tree gets `nan`.
- `oob_score(tree_predictions, bootstrap_indices, y, task="classification")` returns accuracy or R² over the covered samples only, and `nan` if no sample is covered. If `y` has zero variance over the covered samples, R² is `1.0` when the residuals are all zero and `0.0` otherwise.
- An unknown `task`, or a different number of trees in the two inputs, raises `ValueError`.
Example:
```python
boot = [[0, 0, 1, 2, 2], [1, 2, 3, 3, 4], [0, 1, 4, 4, 4]]
preds = [[0, 0, 0, 1, 1], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
oob_predict(preds, boot) # [1., nan, 1., 0., 1.] (sample 3: 1-1 tie -> 0)
oob_score(preds, boot, [1, 0, 0, 1, 1]) # 0.5 (sample 1 excluded)
```
```hint Mark what each tree saw
Marking the samples each tree drew is easier than listing the ones it missed. Repeated indices in a bootstrap should not need special handling.
```
```hint Before you compare with scikit-learn
Count the samples that have no OOB tree, then check what `oob_prediction_` holds for those rows.
```
### Constraints & Assumptions
- `task` is `"classification"` (default) or `"regression"`.
- Predictions and class labels are numeric, since `oob_predict` returns floats.
- Bootstrap indices lie in `[0, n_samples)` and may repeat. Every tree's bootstrap has the same length. A 1-D input is treated as a single tree.
- Regression results must match scikit-learn's `oob_prediction_` and `oob_score_`, to floating-point precision, whenever every sample is out-of-bag for at least one tree.
### What a Strong Answer Covers
- Building the OOB mask without a Python loop over samples, with repeated bootstrap indices handled correctly.
- Aggregating only the OOB trees for each sample in both tasks, with the lowest-label tie rule for votes.
- Consistent treatment of uncovered samples: `nan` in the predictions, left out of the score, and the case where nothing is covered.
- The R² guard for a constant target and the input validation.
- When the regression output agrees with scikit-learn, and why it differs when some samples are uncovered.
### Follow-up Questions
- Why is roughly 36.8% of the data out-of-bag for each tree?
- When some samples have no OOB tree, your regression score and scikit-learn's `oob_score_` differ. Why, and which number would you trust?
- Your classification result can differ from `RandomForestClassifier.oob_score_` even when every sample is covered. What does scikit-learn do differently?
- With 1,000 samples, about how many trees do you need before the chance that any sample is uncovered drops below 1%?
Overview: Implement out-of-bag masks, predictions and scores for a random forest from per-tree predictions and bootstrap indices, with majority voting for classification and averaging for regression. It tests bootstrap sampling, handling of samples no tree left out, the R² edge case for a constant target, and agreement with scikit-learn.