Blending Classifier with Best-Member Fallback
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `BlendingClassifier(base_estimators, meta_estimator, holdout_fraction=0.2)`. `fit(X, y)` trains cloned base models on the first rows, fits a cloned meta-model on their holdout probabilities, and keeps the meta-model only if its holdout log-loss is no worse than the best base model's. Otherwise `predict_proba` and `predict` use that best base model.
Requirements:
- No shuffling. With `n_hold = round(holdout_fraction * n)`, train on the first `n - n_hold` rows and use the rest as the holdout. Raise `ValueError` if `n_hold < 1` or `n_hold >= n`.
- Log-loss is the mean of `-log(clip(p_true, 1e-15, 1))`, where `p_true` is the probability given to the true class. Store `holdout_losses_` (an array in member order), `best_index_` (the argmin, first index on ties) and `meta_loss_`.
- The meta features are the horizontal stack of each base model's `predict_proba` in member order, with columns aligned to `classes_ = np.unique(y)`. `meta_loss_` is computed on the same holdout the meta-model was fit on.
- `use_meta_` is a Python `bool` equal to `meta_loss_ <= holdout_losses_[best_index_]`, so a tie keeps the meta-model.
- Expose the fitted clones as `base_estimators_` and `meta_estimator_`. `predict` returns `classes_[argmax(proba, axis=1)]`.
Example, where `ConstantProba(p)` is a test stub that ignores its input and returns the probabilities `p` for every row:
```python
X = np.zeros((10, 1))
y = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
bases = [ConstantProba((0.7, 0.3)), ConstantProba((0.5, 0.5))]
meta = ConstantProba((0.9, 0.1))
model = BlendingClassifier(bases, meta).fit(X, y)
# holdout = last 2 rows, labels [0, 1]
# model.holdout_losses_ -> [0.7803, 0.6931]
# model.best_index_ -> 1
# model.meta_loss_ -> 1.2040
# model.use_meta_ -> False (predict_proba returns [0.5, 0.5] rows)
```
The first base model scores $(-\ln 0.7 - \ln 0.3)/2 \approx 0.7803$ on the two holdout rows. The meta-model's loss of 1.2040 is worse than the best member's 0.6931, so the classifier falls back to member 1.
```hint Column alignment
A base model fit on the first rows may not have seen every class, so its `predict_proba` can have fewer columns than `classes_`. Map its columns through its own `classes_` before you score or stack them.
```
```hint Which rows each model sees
Base models see only the training rows. The meta-model and every loss use only the holdout rows. Keep the label indices for the holdout in one place so all losses read the same column.
```
### Constraints & Assumptions
- Rows are used in the given order, with no shuffling.
- Estimators follow the scikit-learn API (`fit`, `predict_proba`, `classes_`), and `sklearn.base.clone` is available.
- Labels can be any sortable values, and `classes_` is `np.unique(y)` over all rows.
### What a Strong Answer Covers
- The deterministic split with `n_hold = round(holdout_fraction * n)` and the `ValueError` when either side would be empty.
- Fitting clones and never the passed estimators, and aligning every `predict_proba` output to `classes_` before scoring or stacking.
- Clipped log-loss per member, the first-index tie rule for `best_index_`, and a meta-model fit and scored on the stacked holdout probabilities.
- The `<=` fallback rule stored as a Python bool, with `predict_proba` and `predict` routed through the chosen model.
- Awareness that scoring the meta-model on the rows it was fit on favors it in the comparison.
### Follow-up Questions
- The meta-model is scored on the rows it was trained on. How does that bias the fallback decision, and how would you fix it?
- How does blending differ from stacking with out-of-fold predictions, and when would you pick each?
- The split does not shuffle. When is that the right choice, and when does it hurt?
Overview: Implement a blending ensemble classifier with scikit-learn estimators: base models trained on the first rows, a meta-model fit on their holdout probabilities, and a fallback to the best single member by log-loss. Tests deterministic splitting, probability column alignment and fair model comparison.