Stacking Classifier Versus Its Base Models

Quick Overview

Build a scikit-learn StackingClassifier from logistic regression, a decision tree, k-nearest neighbors and Gaussian naive Bayes, then compare its cross-validated accuracy with each base model on the same stratified folds. It tests stacking mechanics, out-of-fold meta-features and fair model comparison.

Stacking Classifier Versus Its Base Models

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `build_stacking_classifier(random_state=0, cv=5)` to return an unfitted `StackingClassifier`. Then implement `compare_with_base_models(X, y, n_splits=5, random_state=0)` to return the mean cross-validated accuracy of each base model and of the stack. Requirements: - Base estimators, in order: `("logreg", LogisticRegression(max_iter=1000))`, `("tree", DecisionTreeClassifier(max_depth=3, random_state=random_state))`, `("knn", KNeighborsClassifier(n_neighbors=5))`, `("nb", GaussianNB())`. - Stack: `final_estimator=LogisticRegression(max_iter=1000)`, `stack_method="predict_proba"`, `passthrough=False`, `cv=cv`. - Score with `StratifiedKFold(n_splits=n_splits, shuffle=False)` and `scoring="accuracy"`, using a fresh clone of each base model. - Score the stack built by `build_stacking_classifier(random_state=random_state)` (default inner `cv=5`). - Return a dict with keys in order `logreg`, `tree`, `knn`, `nb`, `stacking`, and values as Python `float` fold means. ```hint Two levels of folds `cross_val_score` accepts a splitter object as `cv`. Think about what the stack does with its own `cv` argument inside each outer training fold. ``` ### Constraints & Assumptions - Inputs may be binary or multiclass. - `X` and `y` may be array-likes. - Hyperparameter tuning is out of scope. ### What a Strong Answer Covers - Builds the four base estimators and the stack with exactly the specified settings and order. - Scores every model with the same unshuffled stratified folds and accuracy, returning Python floats in the required key order. - Explains how the stack's internal cross-validation produces out-of-fold probabilities for the meta-learner. - Notes how many meta-features the meta-learner sees in the binary and multiclass cases. - Discusses cost and small-data edge cases, and why the stack is not guaranteed to beat its best base model. ### Follow-up Questions - Why is the meta-learner trained on out-of-fold predictions and not on predictions over the rows the base models were fit on? - When can the stack score below its best base model? - What changes if you set `passthrough=True` or `stack_method="predict"`? - How would you check that the stack's gain over the best base model is real and not fold-to-fold noise?

Overview: Build a scikit-learn StackingClassifier from logistic regression, a decision tree, k-nearest neighbors and Gaussian naive Bayes, then compare its cross-validated accuracy with each base model on the same stratified folds. It tests stacking mechanics, out-of-fold meta-features and fair model comparison.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement build_stacking_classifier(random_state=0, cv=5) to return an unfitted StackingClassifier. Then implement compare_with_base_models(X, y, n_splits=5, random_state=0) to return the mean cross-validated accuracy of each base model and of the stack.

Requirements:

  • Base estimators, in order: ("logreg", LogisticRegression(max_iter=1000)) , ("tree", DecisionTreeClassifier(max_depth=3, random_state=random_state)) , ("knn", KNeighborsClassifier(n_neighbors=5)) , ("nb", GaussianNB()) .
  • Stack: final_estimator=LogisticRegression(max_iter=1000) , stack_method="predict_proba" , passthrough=False , cv=cv .
  • Score with StratifiedKFold(n_splits=n_splits, shuffle=False) and scoring="accuracy" , using a fresh clone of each base model.
  • Score the stack built by build_stacking_classifier(random_state=random_state) (default inner cv=5 ).
  • Return a dict with keys in order logreg , tree , knn , nb , stacking , and values as Python float fold means.

Constraints & Assumptions

  • Inputs may be binary or multiclass.
  • X and y may be array-likes.
  • Hyperparameter tuning is out of scope.

What a Strong Answer Covers Guidance

  • Builds the four base estimators and the stack with exactly the specified settings and order.
  • Scores every model with the same unshuffled stratified folds and accuracy, returning Python floats in the required key order.
  • Explains how the stack's internal cross-validation produces out-of-fold probabilities for the meta-learner.
  • Notes how many meta-features the meta-learner sees in the binary and multiclass cases.
  • Discusses cost and small-data edge cases, and why the stack is not guaranteed to beat its best base model.

Follow-up Questions Guidance

  • Why is the meta-learner trained on out-of-fold predictions and not on predictions over the rows the base models were fit on?
  • When can the stack score below its best base model?
  • What changes if you set passthrough=True or stack_method="predict" ?
  • How would you check that the stack's gain over the best base model is real and not fold-to-fold noise?