import matplotlib.pyplot as plt
import pytorch_lightning as pl
from neuralforecast.models.mlp import MLP
from neuralforecast.models.nbeats import NBEATS
from neuralforecast.utils import AirPassengersDF
Core
NeuralForecast
NeuralForecast (models:List[Any], freq:str, trainers:List[Any]=None)
The core.StatsForecast
class allows you to efficiently fit multiple NeuralForecast
models for large sets of time series. It operates with pandas DataFrame df
that identifies series and datestamps with the unique_id
and ds
columns. The y
column denotes the target time series variable.
Parameters:
models
: List[typing.Any], list of instantiated objects models.StatsForecast.
freq
: str, frequency of the data, panda’s available frequencies.
trainers
: List[typing.Any], optional list of instantiated pytorch lightning trainers.
NeuralForecast.fit
NeuralForecast.fit (df:Optional[pandas.core.frame.DataFrame], val_size:Optional[int]=None, sort_df:bool=True, **data_kwargs)
Fit the core.NeuralForecast.
Fit models
to a large set of time series from DataFrame df
. and store fitted models for later inspection.
Parameters:
df
: pandas.DataFrame, with columns [unique_id
, ds
, y
] and exogenous.
val_size
: int, size of validation set.
sort_df
: bool, sort df before fitting.
Returns:
self
: Returns with stored NeuralForecast
fitted models
.
NeuralForecast.predict
NeuralForecast.predict (df:Optional[pandas.core.frame.DataFrame]=None)
Predict with core.NeuralForecast.
Use stored fitted models
to predict large set of time series from DataFrame df
.
Parameters:
df
: pandas.DataFrame, with [unique_id
, ds
] columns and df
’s future exogenous.
Returns:
fcsts_df
: pandas.DataFrame, with models
columns for point predictions.
NeuralForecast.cross_validation
NeuralForecast.cross_validation (df:pandas.core.frame.DataFrame=None, n_windows:int=1, step_size:int=1, test_size:Optional[int]=None, sort_df:bool=True, **data_kwargs)
Temporal Cross-Validation with core.NeuralForecast.
core.NeuralForecast
’s cross-validation efficiently fits a list of NeuralForecast models through multiple windows, in either chained or rolled manner.
Parameters:
df
: pandas.DataFrame, with columns [unique_id
, ds
, y
] and exogenous.
n_windows
: int, number of windows used for cross validation.
step_size
: int = 1, step size between each window.
test_size
: Optional[int] = None, length of test size. If passed, set n_windows=None
.
Returns:
fcsts_df
: pandas.DataFrame, with insample models
columns for point predictions and probabilistic predictions for all fitted models
.
= NeuralForecast(
fcst =[NBEATS(input_size=12, h=12),
models=12, h=12, step_size=12)],
MLP(input_size=[pl.Trainer(enable_progress_bar=False, max_epochs=100),
trainers=False, max_epochs=100)],
pl.Trainer(enable_progress_bar='M'
freq
)
fcst.fit(AirPassengersDF)= fcst.predict()
forecasts
= plt.subplots(1, 1, figsize = (20, 7))
fig, ax = pd.concat([AirPassengersDF, forecasts]).set_index('ds')
plot_df
'y', 'NBEATS', 'MLP']].plot(ax=ax, linewidth=2)
plot_df[[
'AirPassengers Forecast', fontsize=22)
ax.set_title('Monthly Passengers', fontsize=20)
ax.set_ylabel('Timestamp [t]', fontsize=20)
ax.set_xlabel(={'size': 15})
ax.legend(prop ax.grid()
= NeuralForecast(
fcst =[NBEATS(input_size=12, h=12),
models=12, h=12, step_size=12)],
MLP(input_size=[pl.Trainer(enable_progress_bar=False, max_epochs=100),
trainers=False, max_epochs=100)],
pl.Trainer(enable_progress_bar='M'
freq
)= fcst.cross_validation(AirPassengersDF, n_windows=3, step_size=2) cv_df
= cv_df['cutoff'].unique()
cutoffs for cutoff in cutoffs:
'cutoff == @cutoff').set_index('ds')[['NBEATS', 'y']].plot() cv_df.query(