Core

NeuralForecast

source

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.


source

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.


source

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.


source

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.

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
fcst = NeuralForecast(
    models=[NBEATS(input_size=12, h=12), 
            MLP(input_size=12, h=12, step_size=12)], 
    trainers=[pl.Trainer(enable_progress_bar=False, max_epochs=100), 
              pl.Trainer(enable_progress_bar=False, max_epochs=100)],
    freq='M'
)
fcst.fit(AirPassengersDF)
forecasts = fcst.predict()

fig, ax = plt.subplots(1, 1, figsize = (20, 7))
plot_df = pd.concat([AirPassengersDF, forecasts]).set_index('ds')

plot_df[['y', 'NBEATS', 'MLP']].plot(ax=ax, linewidth=2)

ax.set_title('AirPassengers Forecast', fontsize=22)
ax.set_ylabel('Monthly Passengers', fontsize=20)
ax.set_xlabel('Timestamp [t]', fontsize=20)
ax.legend(prop={'size': 15})
ax.grid()
fcst = NeuralForecast(
    models=[NBEATS(input_size=12, h=12), 
            MLP(input_size=12, h=12, step_size=12)], 
    trainers=[pl.Trainer(enable_progress_bar=False, max_epochs=100), 
              pl.Trainer(enable_progress_bar=False, max_epochs=100)],
    freq='M'
)
cv_df = fcst.cross_validation(AirPassengersDF, n_windows=3, step_size=2)
cutoffs = cv_df['cutoff'].unique()
for cutoff in cutoffs:
    cv_df.query('cutoff == @cutoff').set_index('ds')[['NBEATS', 'y']].plot()