Save and load forecasters¶
Skforecast models can be easily saved and loaded from disk using the pickle or joblib library. To streamline this process, two convenient functions, save_forecaster
and load_forecaster
, are available. See below for a simple example.
A forecaster_id
has been included when initializing the forecaster, this may help to identify the target of the forecaster.
  Tip
Since version 0.7.0, when initializing the forecaster, a forecaster_id
can be included to help identify the target of the forecaster.
  Note
Learn how to use forecaster models in production.
Libraries¶
In [1]:
Copied!
# Libraries
# ==============================================================================
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from skforecast.ForecasterAutoreg import ForecasterAutoreg
from skforecast.utils import save_forecaster
from skforecast.utils import load_forecaster
# Libraries
# ==============================================================================
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from skforecast.ForecasterAutoreg import ForecasterAutoreg
from skforecast.utils import save_forecaster
from skforecast.utils import load_forecaster
Data¶
In [2]:
Copied!
# Download data
# ==============================================================================
url = (
'https://raw.githubusercontent.com/JoaquinAmatRodrigo/skforecast/master/'
'data/h2o.csv'
)
data = pd.read_csv(url, sep=',', header=0, names=['y', 'date'])
data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')
data = data.set_index('date')
data = data.asfreq('MS')
# Download data
# ==============================================================================
url = (
'https://raw.githubusercontent.com/JoaquinAmatRodrigo/skforecast/master/'
'data/h2o.csv'
)
data = pd.read_csv(url, sep=',', header=0, names=['y', 'date'])
data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')
data = data.set_index('date')
data = data.asfreq('MS')
Save and load forecaster model¶
In [3]:
Copied!
# Create and train forecaster
forecaster = ForecasterAutoreg(
regressor = RandomForestRegressor(random_state=123),
lags = 5,
forecaster_id = "forecaster_001"
)
forecaster.fit(y=data['y'])
forecaster.predict(steps=3)
# Create and train forecaster
forecaster = ForecasterAutoreg(
regressor = RandomForestRegressor(random_state=123),
lags = 5,
forecaster_id = "forecaster_001"
)
forecaster.fit(y=data['y'])
forecaster.predict(steps=3)
Out[3]:
2008-07-01 0.714526 2008-08-01 0.789144 2008-09-01 0.818433 Freq: MS, Name: pred, dtype: float64
In [4]:
Copied!
# Save model
save_forecaster(forecaster, file_name='forecaster_001.py', verbose=False)
# Save model
save_forecaster(forecaster, file_name='forecaster_001.py', verbose=False)
In [5]:
Copied!
# Load model
forecaster_loaded = load_forecaster('forecaster_001.py', verbose=True)
# Load model
forecaster_loaded = load_forecaster('forecaster_001.py', verbose=True)
================= ForecasterAutoreg ================= Regressor: RandomForestRegressor(random_state=123) Lags: [1 2 3 4 5] Transformer for y: None Transformer for exog: None Window size: 5 Weight function included: False Exogenous included: False Type of exogenous variable: None Exogenous variables names: None Training range: [Timestamp('1991-07-01 00:00:00'), Timestamp('2008-06-01 00:00:00')] Training index type: DatetimeIndex Training index frequency: MS Regressor parameters: {'bootstrap': True, 'ccp_alpha': 0.0, 'criterion': 'squared_error', 'max_depth': None, 'max_features': 1.0, 'max_leaf_nodes': None, 'max_samples': None, 'min_impurity_decrease': 0.0, 'min_samples_leaf': 1, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'n_estimators': 100, 'n_jobs': None, 'oob_score': False, 'random_state': 123, 'verbose': 0, 'warm_start': False} fit_kwargs: {} Creation date: 2023-07-09 13:57:47 Last fit date: 2023-07-09 13:57:47 Skforecast version: 0.9.0 Python version: 3.11.4 Forecaster id: forecaster_001
In [6]:
Copied!
# Predict
forecaster_loaded.predict(steps=3)
# Predict
forecaster_loaded.predict(steps=3)
Out[6]:
2008-07-01 0.714526 2008-08-01 0.789144 2008-09-01 0.818433 Freq: MS, Name: pred, dtype: float64
In [7]:
Copied!
# Forecaster identifier
# ==============================================================================
forecaster.forecaster_id
# Forecaster identifier
# ==============================================================================
forecaster.forecaster_id
Out[7]:
'forecaster_001'
In [8]:
Copied!
%%html
<style>
.jupyter-wrapper .jp-CodeCell .jp-Cell-inputWrapper .jp-InputPrompt {display: none;}
</style>
%%html