Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Ensemble StackingClassifier

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Ensemble_Methods, Stacking
Last Updated 2026-02-08 16:00 GMT

Overview

Stacking classifier uses a meta-classifier to learn optimal combinations of base classifier predictions for improved binary classification performance.

Description

This implementation trains multiple base classifiers and a meta-classifier in parallel. For each instance, base classifiers first make predictions (out-of-fold style), then update themselves. The meta-classifier learns from these predictions, optionally augmented with original features. Predictions from each base model are formatted as "oof_i_k" features where i is the model index and k is the class label. This prevents information leakage while allowing the meta-model to learn which base models to trust in different scenarios.

Usage

Use stacking when you have multiple diverse base classifiers and want to learn their optimal combination. It's particularly effective when base models have complementary strengths. Set include_features=True to give the meta-classifier access to both predictions and original features, or False to rely only on base model outputs. Choose a simple, fast meta-classifier like LogisticRegression to avoid overfitting.

Code Reference

Source Location

Signature

class StackingClassifier(base.Ensemble, base.Classifier):
    def __init__(
        self,
        models: list[base.Classifier],
        meta_classifier: base.Classifier,
        include_features=True,
    ):
        super().__init__(models)
        self.meta_classifier = meta_classifier
        self.include_features = include_features

Import

from river import ensemble

I/O Contract

Parameters

Parameter Type Default Description
models list[Classifier] required Base classifiers for level 0
meta_classifier Classifier required Meta-classifier for level 1
include_features bool True Pass original features to meta-classifier

Attributes

Attribute Type Description
models list Base classifier instances
meta_classifier Classifier Meta-classifier instance

Input/Output

Method Input Output
learn_one x: dict, y: Any None
predict_proba_one x: dict dict[Any, float]
predict_one x: dict Any

Usage Examples

from river import compose
from river import datasets
from river import ensemble
from river import evaluate
from river import linear_model as lm
from river import metrics
from river import preprocessing as pp

dataset = datasets.Phishing()

model = compose.Pipeline(
    ('scale', pp.StandardScaler()),
    ('stack', ensemble.StackingClassifier(
        [
            lm.LogisticRegression(),
            lm.PAClassifier(mode=1, C=0.01),
            lm.PAClassifier(mode=2, C=0.01),
        ],
        meta_classifier=lm.LogisticRegression()
    ))
)

metric = metrics.F1()

evaluate.progressive_val_score(dataset, model, metric)
# F1: 88.14%

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment