Overview
The Regressor class is an abstract base class that defines the interface for all regression algorithms in River, supporting both single-instance and mini-batch learning.
Description
The Regressor class extends Estimator to provide the standard interface for regression models in River. It defines two abstract methods that all regressors must implement: learn_one for updating the model with a single example consisting of features and a numeric target, and predict_one for predicting a numeric output given features. The MiniBatchRegressor subclass extends this interface to support vectorized operations on pandas DataFrames through learn_many and predict_many methods, enabling efficient batch processing when available.
Usage
Use Regressor as the parent class when implementing new regression algorithms that learn from individual examples. Extend MiniBatchRegressor instead if your algorithm can efficiently process multiple examples simultaneously. All regressors must implement both learn_one and predict_one methods.
Code Reference
Source Location
Signature
class Regressor(estimator.Estimator):
"""A regressor."""
@abc.abstractmethod
def learn_one(self, x: dict[base.typing.FeatureName, Any], y: base.typing.RegTarget) -> None
@abc.abstractmethod
def predict_one(self, x: dict[base.typing.FeatureName, Any]) -> base.typing.RegTarget
class MiniBatchRegressor(Regressor):
"""A regressor that can operate on mini-batches."""
@abc.abstractmethod
def learn_many(self, X: pd.DataFrame, y: pd.Series) -> None
@abc.abstractmethod
def predict_many(self, X: pd.DataFrame) -> pd.Series
Import
from river.base import Regressor, MiniBatchRegressor
I/O Contract
Regressor.learn_one
| Parameter |
Type |
Description
|
| x |
dict[FeatureName, Any] |
Dictionary of features
|
| y |
RegTarget |
The numeric target value (typically float or int)
|
Regressor.predict_one
| Parameter |
Type |
Description
|
| x |
dict[FeatureName, Any] |
Dictionary of features
|
| Returns |
Type |
Description
|
| prediction |
RegTarget |
The predicted numeric value
|
MiniBatchRegressor.learn_many
| Parameter |
Type |
Description
|
| X |
pd.DataFrame |
DataFrame of features where each row is an example
|
| y |
pd.Series |
Series of numeric target values
|
MiniBatchRegressor.predict_many
| Parameter |
Type |
Description
|
| X |
pd.DataFrame |
DataFrame of features where each row is an example
|
| Returns |
Type |
Description
|
| predictions |
pd.Series |
Series of predicted numeric values
|
Usage Examples
from river import linear_model
from river import datasets
from river import metrics
# Create a regressor
model = linear_model.LinearRegression()
# Single instance learning
metric = metrics.MAE()
for x, y in datasets.TrumpApproval():
# Make a prediction
y_pred = model.predict_one(x)
# Update metric
metric.update(y, y_pred)
# Update the model
model.learn_one(x, y)
print(f'MAE: {metric.get():.3f}')
# Implementing a custom regressor
from river.base import Regressor
class MeanRegressor(Regressor):
def __init__(self):
self.mean = 0.0
self.n = 0
def learn_one(self, x, y):
# Update running mean
self.n += 1
self.mean += (y - self.mean) / self.n
def predict_one(self, x):
# Always predict the mean
return self.mean
# Use the custom regressor
model = MeanRegressor()
for x, y in datasets.TrumpApproval().take(100):
y_pred = model.predict_one(x)
model.learn_one(x, y)
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.