Implementation:Online ml River ModelSelection Base
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Model_Selection, Software_Architecture |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Base classes for model selection strategies that define the common interface and validation logic for comparing multiple models online.
Description
These abstract base classes (ModelSelector, ModelSelectionRegressor, ModelSelectionClassifier) provide the foundation for all model selection implementations in River. They inherit from Ensemble and maintain a collection of candidate models. The base class validates that the provided metric is compatible with all candidate models during initialization. Each subclass must implement a best_model property that returns the currently selected model. Prediction methods delegate to the best model or fall back to the first model if no best model has been determined yet. The classes also provide default unit test parameters for consistent testing.
Usage
These are base classes not intended for direct instantiation. Use them as parent classes when implementing custom model selection strategies. The classes ensure consistent interfaces across different selection methods (bandit, successive halving, greedy, etc.). When creating a custom selector, inherit from ModelSelectionRegressor or ModelSelectionClassifier and implement the best_model property and learn_one method with your selection logic.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/model_selection/base.py
Signature
class ModelSelector(base.Ensemble, ABC):
def __init__(self, models: Iterator[base.Estimator], metric: metrics.base.Metric)
@property
@abstractmethod
def best_model(self):
"""The current best model."""
class ModelSelectionRegressor(ModelSelector, base.Regressor):
def predict_one(self, x)
class ModelSelectionClassifier(ModelSelector, base.Classifier):
def predict_proba_one(self, x)
Import
from river.model_selection import base
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| models | Iterator[Estimator] | Collection of candidate models |
| metric | Metric | Performance metric compatible with all models |
| Property/Method | Return Type | Description |
|---|---|---|
| best_model | Estimator | Currently selected best model (abstract) |
| predict_one(x) | Any | Delegates to best_model.predict_one |
| predict_proba_one(x) | dict | Delegates to best_model.predict_proba_one |
Usage Examples
# This is a base class - use concrete implementations instead
from river import model_selection
# Example concrete usage:
from river import linear_model
from river import metrics
from river import optim
from river import preprocessing
models = [
preprocessing.StandardScaler() |
linear_model.LinearRegression(optimizer=optim.SGD(lr=lr))
for lr in [1e-4, 1e-3, 1e-2, 1e-1]
]
# Use a concrete implementation like GreedyRegressor
selector = model_selection.GreedyRegressor(models, metrics.MAE())