Build your first stacked ensemble

Ensemble Methods in Python

Román de las Heras

Data Scientist, Appodeal

Stacking models with scikit-learn

Some features of stacking implementation from scikit-learn:

  1. scikit-learn provides stacking estimators (since version 0.22)
  2. Compatible with other scikit-learn estimators
  3. The final estimator is trained through cross-validation
Ensemble Methods in Python

General Steps

Stacking-Architecture-Steps.png

General steps for the implementation:

  1. Prepare the dataset
  2. Build the first-layer estimators
  3. Append the predictions to the dataset
  4. Build the second-layer meta estimator
  5. Use the stacked ensemble for predictions
Ensemble Methods in Python

Stacking classifier

from sklearn.ensemble import StackingClassifier
# Instantiate the 1st-layer classifiers
classifiers = [
    ('clf1', Classifier1(params1)),
    ('clf2', Classifier2(params2)),
    ...
    ('clfN', ClassifierN(paramsN))
]
# Instantiate the 2nd-layer classifier
clf_meta = ClassifierMeta(paramsMeta)
# Build the Stacking classifier
clf_stack = StackingClassifier(
   estimators=classifiers,
   final_estimator=clf_meta,
   cv=5,
   stack_method='predict_proba',
   passthrough=False)
# Use the fit and predict methods
clf_stack.fit(X_train, y_train)
pred = clf_stack.predict(X_test)
Ensemble Methods in Python

Stacking regressor

from sklearn.ensemble import StackingRegressor
# Instantiate the 1st-layer regressors
regressors = [
    ('reg1', Regressor1(params1)),
    ('reg2', Regressor2(params2)),
    ...
    ('regN', RegressorN(paramsN))    
]
# Instantiate the 2nd-layer regressor
reg_meta = RegressorMeta(paramsMeta)
# Build the Stacking regressor
reg_stack = StackingRegressor(
   estimators=regressors,
   final_estimator=reg_meta,
   cv=5,
   passthrough=False)
# Use the fit and predict methods
reg_stack.fit(X_train, y_train)
pred = reg_stack.predict(X_test)
Ensemble Methods in Python

It's your turn!

Ensemble Methods in Python

Preparing Video For Download...