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 TimeSeries Forecaster Base

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Time_Series, Forecasting
Last Updated 2026-02-08 16:00 GMT

Overview

Abstract base class defining the interface for time series forecasting models in River.

Description

Defines the Forecaster interface with learn_one and forecast methods. learn_one accepts an endogenous variable (y) and optional exogenous variables (x). forecast generates predictions for a specified horizon, optionally using future exogenous variables. All River forecasters inherit from this base class.

Usage

Use as the parent class when implementing custom time series forecasters. Ensures compatibility with River's ecosystem and provides a consistent interface for all forecasting models.

Code Reference

Source Location

Signature

class Forecaster(base.Estimator):
    @property
    def _supervised(self):
        return True

    @abc.abstractmethod
    def learn_one(self, y: float, x: dict | None = None) -> None:
        """Updates the model.

        Parameters
        ----------
        y
            The endogenous variable
        x
            Optional exogenous variables
        """
        ...

    @abc.abstractmethod
    def forecast(self, horizon: int, xs: list[dict] | None = None) -> list:
        """Makes forecast at each step of the given horizon.

        Parameters
        ----------
        horizon
            Number of steps ahead to forecast
        xs
            Optional future exogenous variables
        """
        ...

Import

from river import time_series

I/O Contract

Method Parameters Returns Description
learn_one y: float, x: dict or None None Updates model with new observation
forecast horizon: int, xs: list[dict] or None list Generates predictions for horizon steps

Usage Examples

from river import time_series
import abc

# Example: Implementing a simple forecaster
class NaiveForecaster(time_series.base.Forecaster):
    """Forecasts the last seen value"""

    def __init__(self):
        self.last_value = 0

    def learn_one(self, y, x=None):
        self.last_value = y

    def forecast(self, horizon, xs=None):
        return [self.last_value] * horizon

# Using the custom forecaster
model = NaiveForecaster()

# Learn from time series
for y in [10, 12, 11, 13, 15]:
    model.learn_one(y)

# Forecast next 3 steps
predictions = model.forecast(horizon=3)
print(f"Forecasts: {predictions}")  # [15, 15, 15]

# Using built-in forecasters
from river import time_series

# SNARIMAX example
snarimax = time_series.SNARIMAX(
    p=1,
    d=0,
    q=0,
    m=12,  # Monthly seasonality
    sp=1
)

# Simulate time series
import math
for t in range(50):
    y = 10 + 2 * math.sin(2 * math.pi * t / 12) + (t % 7) * 0.5
    snarimax.learn_one(y)

# Forecast
future = snarimax.forecast(horizon=5)
print(f"SNARIMAX forecast: {[f'{v:.2f}' for v in future]}")

Related Pages

Page Connections

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