Implementation:Online ml River Preprocessing PredClipper
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Preprocessing, Regression |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Wrapper for regression models that clips predictions to a specified range.
Description
PredClipper constrains regression model predictions to lie within specified minimum and maximum bounds. It wraps any regression model and applies clamping to the predicted values after inference, ensuring they remain within valid ranges. The underlying model trains normally without constraints, but all predictions are clipped to [y_min, y_max] before being returned. This is particularly useful when domain knowledge dictates hard limits on possible output values.
Usage
Use this when your regression target has known physical or logical bounds (e.g., percentages between 0-100, prices above zero, ratings within 1-5). Prevents models from making nonsensical predictions outside valid ranges. Common applications include predicting probabilities, counts, or any domain-constrained continuous variables. Wraps any regressor without modifying its learning behavior.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/preprocessing/pred_clipper.py
Signature
class PredClipper(base.Wrapper, base.Regressor):
def __init__(self, regressor: base.Regressor, y_min: float, y_max: float)
Import
from river import preprocessing
I/O Contract
| Input | Output |
|---|---|
| Dict[str, float] - Features | float - Clipped prediction |
Usage Examples
from river import linear_model
from river import preprocessing
dataset = (
({'a': 2, 'b': 4}, 80),
({'a': 3, 'b': 5}, 100),
({'a': 4, 'b': 6}, 120)
)
model = preprocessing.PredClipper(
regressor=linear_model.LinearRegression(),
y_min=0,
y_max=200
)
for x, y in dataset:
model.learn_one(x, y)
print(model.predict_one({'a': -100, 'b': -200}))
# 0
print(model.predict_one({'a': 50, 'b': 60}))
# 200