Skip to content

How to Save and Load Forecasters

This guide shows you how to serialize trained forecasters to disk and load them in a new session to produce predictions without retraining.

Prerequisites

Try it interactively

  • How to Save and Load Forecasters


    Serialize fitted forecasters with joblib and pickle, reload them in a fresh session, and produce predictions without retraining.

    View ยท Open in marimo

1. Save a Fitted Forecaster

The example uses PointReductionForecaster, but the same approach applies to any Yohou estimator. Because every Yohou forecaster extends sklearn's BaseEstimator and stores all state as Python attributes, standard serializers capture the complete fitted object (see Core Concepts for how this state is structured).

Use joblib to serialize the forecaster after fitting. joblib handles large NumPy arrays more efficiently than pickle and is the recommended approach for scikit-learn compatible estimators:

import joblib
from sklearn.linear_model import Ridge
from yohou.datasets import fetch_electricity_demand
from yohou.model_selection import train_test_split
from yohou.point import PointReductionForecaster

data = fetch_electricity_demand()
y_train, y_test = train_test_split(data.frame, test_size=48)

forecaster = PointReductionForecaster(estimator=Ridge())
forecaster.fit(y_train, forecasting_horizon=48)

joblib.dump(forecaster, "forecaster.joblib")

2. Load and Predict

Load the saved forecaster and call predict without calling fit again:

import joblib

forecaster = joblib.load("forecaster.joblib")
y_pred = forecaster.predict(forecasting_horizon=48)

The loaded forecaster retains all fitted state: learned parameters, observation history, and pipeline transformers.

3. Use pickle as an Alternative

Standard library pickle works for all yohou forecasters. Prefer joblib when the estimator contains large arrays; use pickle when you want to avoid the extra dependency:

import pickle

# Save
with open("forecaster.pkl", "wb") as f:
    pickle.dump(forecaster, f)

# Load
with open("forecaster.pkl", "rb") as f:
    forecaster = pickle.load(f)

Security: never load untrusted pickle files

Both pickle and joblib files can execute arbitrary code when loaded. Only load files you created yourself or received from a trusted source. For sharing across trust boundaries, consider exporting predictions as CSV or Parquet instead of serialized objects.

Version Compatibility

A saved forecaster is tied to the versions of yohou and its dependencies (scikit-learn, polars) that were installed when the file was created. Loading a forecaster saved with a different version may raise errors or produce silent changes in behaviour. Pin versions in your deployment environment and re-save the forecaster whenever you upgrade.

See Also