Implementation:Online ml River ModelSelection Greedy
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Model_Selection, Hyperparameter_Tuning |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Greedy model selection updates all candidate models at every step and uses the current best performer for predictions, providing a simple exhaustive comparison strategy.
Description
GreedyRegressor trains all provided models on every incoming sample, maintaining separate performance metrics for each. After each update, it checks if any model has surpassed the current best and updates the best_model reference accordingly. The metric is evaluated on each model's prediction before training to track progressive performance. This approach is "greedy" in the sense that it uses maximum computational resources by updating all models continuously, unlike bandit methods that selectively update models or successive halving that eliminates candidates.
Usage
Use greedy selection when you have sufficient computational resources and want to ensure no model is prematurely discarded. It is most appropriate when you have a small number of candidate models (3-10) and want continuous comparison without elimination. The approach guarantees that at any point, you are using the truly best model based on all available data. It is less efficient than bandit or successive halving methods but provides the most thorough comparison.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/model_selection/greedy.py
Signature
class GreedyRegressor(
models: list[base.Regressor],
metric: metrics.base.RegressionMetric | None = None
)
Import
from river import model_selection
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| models | list[Regressor] | List of regression models to compare |
| x | dict | Feature dictionary |
| y | float | Target value for regression |
| Method | Return Type | Description |
|---|---|---|
| predict_one(x) | float | Prediction from current best model |
| learn_one(x, y) | None | Updates all models and tracks best performer |
| best_model | Regressor | Current best performing model |
Usage Examples
from river import datasets
from river import evaluate
from river import linear_model
from river import metrics
from river import model_selection
from river import optim
from river import preprocessing
# Create candidate models with different learning rates
models = [
linear_model.LinearRegression(optimizer=optim.SGD(lr=lr))
for lr in [1e-5, 1e-4, 1e-3, 1e-2]
]
# Apply greedy selection
model = (
preprocessing.StandardScaler() |
model_selection.GreedyRegressor(models, metrics.MAE())
)
# Evaluate on a dataset
result = evaluate.progressive_val_score(
datasets.TrumpApproval(),
model,
metrics.MAE()
)
print(result) # MAE: 1.319678