Master stacking in scikit-learn. Learn to use meta-learners to combine heterogeneous model predictions with cross-validated training to prevent leakage.
Previously in this course, we explored Model Ensembling: Voting and Averaging for Robust ML Pipelines, where we combined models using simple arithmetic or majority rules. While effective, voting treats all base models as equals. Today, we move beyond simple aggregation by implementing stacking, an ensemble method that trains a "meta-learner" to optimally weigh the predictions of heterogeneous base models.
Stacking (Stacked Generalization) is conceptually elegant: instead of averaging predictions, we use the output of multiple base models as the input features for a final meta-model. If you have a Random Forest, an SVM, and a Gradient Boosting model, you can train a Logistic Regression to learn which model is most reliable for different regions of your feature space.
The core challenge in stacking is preventing data leakage. If you train your meta-learner on the same predictions the base models used to train themselves, the meta-learner will likely overfit to the base models' training errors. To solve this, we use out-of-fold (OOF) predictions.
We will use scikit-learn's StackingClassifier to orchestrate this. It handles the cross-validation logic internally, ensuring that the meta-learner receives clean, unbiased input.
PYTHONfrom sklearn.ensemble import StackingClassifier, RandomForestClassifier, GradientBoostingClassifier from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split # 1. Prepare data X, y = make_classification(n_samples=1000, n_features=20, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 2. Define base learners base_models = [ (CE9178">'rf', RandomForestClassifier(n_estimators=50)), (CE9178">'gb', GradientBoostingClassifier()), (CE9178">'svc', SVC(probability=True)) # SVC needs probability=True for stacking ] # 3. Define the meta-learner # We use a simple LogisticRegression to learn how to combine the inputs meta_learner = LogisticRegression() # 4. Initialize and fit the stack stack = StackingClassifier( estimators=base_models, final_estimator=meta_learner, cv=5, # 5-fold cross-validation for generating OOF predictions n_jobs=-1 ) stack.fit(X_train, y_train) print(f"Stacking Accuracy: {stack.score(X_test, y_test):.4f}")
In this example, StackingClassifier automates the complex task of managing the cross-validation folds. Notice that we set probability=True for the SVC. If your base learners don't provide probability estimates, the stack will default to using hard class labels, which usually yields inferior results.
Using the project code we established in Project Milestone: Tuning the Champion Model, replace your current champion model with a StackingClassifier.
StackingClassifier.predict_proba) rather than hard labels. The meta-learner needs the confidence scores to make nuanced decisions.Stacking is a powerful ensemble technique that treats model predictions as features. By utilizing cross-validated out-of-fold predictions, we generate a robust meta-dataset that allows the meta-learner to weigh the strengths of our base models effectively. Always prioritize model diversity and start with simple meta-learners to avoid overfitting your stack.
Up next: We will explore Blending, a manual alternative to stacking that provides more control over the data split used for the meta-learner.
Learn to boost model performance with ensemble methods. We cover implementing VotingClassifier and VotingRegressor to combine diverse models effectively.
Read moreMaster pipeline serialization with Joblib. Learn to save and load your Scikit-Learn pipelines for reliable inference and production-ready deployments.
Stacking Architectures
Monitoring Data Drift
Tracking Performance Degradation
Logging and Observability
Automated Retraining Triggers
Containerization Basics
Handling Environment Parity
Documentation for Production
Project Milestone: Deployment Readiness