Stacking Classifier with Out-of-Fold Meta-Features
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `StackingClassifier(estimators, n_splits=5, meta_C=1.0)`, where `estimators` is a list of `(name, estimator)` tuples. `fit(X, y, folds=None)` builds leakage-free out-of-fold probability features, trains a logistic meta-learner on them, and refits the base estimators on all data. `fit` returns `self`.
The class must meet these requirements:
- Use `folds` (one int id per row) if it is given, else contiguous blocks from `np.array_split(np.arange(n), n_splits)`. A `folds` argument with the wrong shape raises `ValueError`.
- For each fold, fit a `clone` of each estimator on the other folds and write its held-out `predict_proba` into `oof_features_`, with rows in original order and column blocks in estimator order.
- Each estimator's block has 1 column for a binary target (the probability of `classes_[1]`) and one column per class, in sorted `classes_` order, for a multiclass target.
- Store the full-data refits in `estimators_` and fit `final_estimator_ = LogisticRegression(C=meta_C, max_iter=1000)` on `oof_features_`.
- `transform(X)` builds the same features from `estimators_`. `predict_proba` returns the meta-learner's probabilities on them, and `predict` returns the argmax class label (labels may be strings).
```hint Which rows each model may see
A row's meta-feature has to come from a model that never trained on that row. For each fold, work out which rows the clone trains on and which rows it predicts.
```
```hint Column layout
Decide the width and order of each estimator's block from the global class list, and check what a single fold model's `classes_` reports before copying its `predict_proba` columns into place.
```
### Constraints & Assumptions
- `X` is a numeric array of shape `(n, d)`. `y` holds `n` labels, which may be integers or strings.
- `folds`, when given, is a 1-D array of `n` integer fold ids. Any other shape raises `ValueError`.
- Without `folds`, the blocks are contiguous and unshuffled, exactly as `np.array_split(np.arange(n), n_splits)` returns them.
- Base estimators are scikit-learn compatible and expose `predict_proba` and `classes_`. `sklearn.base.clone` and `LogisticRegression` may be used.
### What a Strong Answer Covers
- Why the meta-learner must train on out-of-fold probabilities, and how in-sample probabilities from the base models would leak.
- Fold assignment from either source, with shape validation and every row receiving exactly one out-of-fold prediction in its original position.
- The feature layout: estimator blocks in order, one column per estimator for binary targets, K columns in sorted class order for multiclass, and alignment when a fold model has seen fewer classes.
- The split between training-time features (fold clones) and inference-time features (full-data refits in `estimators_`).
- A `predict` that returns the original labels, including strings, instead of column indices.
### Follow-up Questions
- Why drop one probability column in the binary case but keep all columns for multiclass?
- What breaks if a training fold is missing a class, and how would you handle it?
- How would you add a `passthrough` option or a second stacking layer?
Overview: Implement a stacking classifier that builds out-of-fold probability features from several scikit-learn base models and trains a logistic regression meta-learner on them. It tests leakage-free cross-validation, class column alignment, and clean fit, transform and predict methods.