### Install datasetsforecast Library Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/datarequirements.html Installs the datasetsforecast library, which is used in the following examples for loading data. ```python %%capture !pip install datasetsforecast ``` -------------------------------- ### Install NeuralForecast and DatasetsForecast Libraries Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/longhorizon_probabilistic.html Installs the necessary libraries for running the examples. This is a prerequisite for the subsequent code. ```python %%capture !pip install neuralforecast datasetsforecast ``` -------------------------------- ### HINT Usage Example: Setup and Data Loading Source: https://nixtlaverse.nixtla.io/neuralforecast/models.hint.html This snippet demonstrates the initial setup for using the HINT model, including importing necessary libraries, defining an auxiliary sorting function, and loading the TourismLarge hierarchical dataset. ```python import matplotlib.pyplot as plt from neuralforecast.losses.pytorch import GMM, sCRPS from datasetsforecast.hierarchical import HierarchicalData # Auxiliary sorting def sort_df_hier(Y_df, S_df): # NeuralForecast core, sorts unique_id lexicographically # by default, this class matches S_df and Y_hat_df order. Y_df.unique_id = Y_df.unique_id.astype('category') Y_df.unique_id = Y_df.unique_id.cat.set_categories(S_df.index) Y_df = Y_df.sort_values(by=['unique_id', 'ds']) return Y_df # Load TourismSmall dataset horizon = 12 Y_df, S_df, tags = HierarchicalData.load('./data', 'TourismLarge') Y_df['ds'] = pd.to_datetime(Y_df['ds']) Y_df = sort_df_hier(Y_df, S_df) level = [80,90] # Instantiate HINT ``` -------------------------------- ### Setup for NeuralForecast Simulation Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/simulation.html Imports necessary libraries and configures logging and warnings for NeuralForecast simulation. Ensure PyTorch is installed with appropriate settings. ```python import logging import warnings import matplotlib.pyplot as plt import numpy as np import pandas as pd import torch warnings.filterwarnings("ignore") logging.getLogger("pytorch_lightning").setLevel(logging.ERROR) torch.set_float32_matmul_precision("high") ``` -------------------------------- ### NeuralForecast Usage Example Setup Source: https://nixtlaverse.nixtla.io/neuralforecast/models.tft.html Imports necessary libraries for using the NeuralForecast library, including plotting and data manipulation tools. ```python import matplotlib.pyplot as plt import numpy as np import pandas as pd from neuralforecast import NeuralForecast ``` -------------------------------- ### Install NeuralForecast and Hyperopt Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/capabilities/hyperparameter_tuning.html Installs the necessary libraries for NeuralForecast and hyperparameter optimization. ```python %%capture !pip install neuralforecast hyperopt ``` -------------------------------- ### Install NeuralForecast and Dependencies Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/longhorizon_nhits.html Installs the NeuralForecast library along with datasetsforecast and utilsforecast. This is the initial step for using the library's functionalities. ```python %%capture !pip install neuralforecast datasetsforecast utilsforecast ``` -------------------------------- ### Install NeuralForecast with Spark Extra Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/installation.html Install NeuralForecast with the 'spark' extra for distributed training capabilities. ```shell pip install neuralforecast[spark] ``` -------------------------------- ### Install Nixtla Libraries Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/comparing_methods.html Installs the necessary libraries for statistical, machine learning, and neural forecasting. ```python %%capture !pip install statsforecast mlforecast neuralforecast pyarrow ``` -------------------------------- ### Install Necessary Packages Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/intermittent_data.html Installs the required libraries for NeuralForecast, StatsForecast, and data handling. It's recommended to use GPU for faster computation. ```python %%capture !pip install statsforecast s3fs fastparquet neuralforecast ``` -------------------------------- ### Install NeuralForecast Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/capabilities/save_load_models.html Installs the NeuralForecast library. This is a prerequisite for using the library's functionalities. ```python %%capture !pip install neuralforecast ``` -------------------------------- ### Install NeuralForecast in Dev Mode Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/introduction.html Clone the repository and install NeuralForecast in editable mode for real-time modifications. ```bash git clone https://github.com/Nixtla/neuralforecast.git cd neuralforecast pip install -e . ``` -------------------------------- ### Custom LightningDataModule Example Source: https://nixtlaverse.nixtla.io/neuralforecast/tsdataset.html Demonstrates how to create a custom LightningDataModule by subclassing L.LightningDataModule. This example includes methods for data preparation, setup, creating data loaders for training, validation, and testing, and handling exceptions and teardown. ```python import lightning.pytorch as L import torch.utils.data as data from pytorch_lightning.demos.boring_classes import RandomDataset class MyDataModule(L.LightningDataModule): def prepare_data(self): # download, IO, etc. Useful with shared filesystems # only called on 1 GPU/TPU in distributed ... def setup(self, stage): # make assignments here (val/train/test split) # called on every process in DDP dataset = RandomDataset(1, 100) self.train, self.val, self.test = data.random_split( dataset, [80, 10, 10], generator=torch.Generator().manual_seed(42) ) def train_dataloader(self): return data.DataLoader(self.train) def val_dataloader(self): return data.DataLoader(self.val) def test_dataloader(self): return data.DataLoader(self.test) def on_exception(self, exception): # clean up state after the trainer faced an exception ... def teardown(self): # clean up state after the trainer stops, delete files... # called on every process in DDP ...* ``` -------------------------------- ### Install NeuralForecast with Pip Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/installation.html Use this command to install the released version of NeuralForecast from PyPI. ```shell pip install neuralforecast ``` -------------------------------- ### Install Neuralforecast and Hyperopt Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/capabilities/time_series_scaling.html Installs the necessary libraries for neural forecasting experiments. The `%%capture` magic command suppresses output. ```python %%capture !pip install neuralforecast !pip install hyperopt ``` -------------------------------- ### Install NeuralForecast in Editable Mode (CPU) Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/installation.html Install NeuralForecast in editable mode with the development dependencies and CPU PyTorch backend using 'uv'. ```bash $ uv pip install -e ".[dev]" --torch-backend cpu ``` -------------------------------- ### Install NeuralForecast with AWS Extra Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/installation.html Install NeuralForecast with the 'aws' extra for saving and loading models from S3. ```shell pip install neuralforecast[aws] ``` -------------------------------- ### RMoK Model Initialization and Prediction Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.rmok.html Example demonstrating how to initialize, train, and predict with the RMoK model using the AirPassengers dataset. Includes plotting of the forecasts. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import RMoK from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic from neuralforecast.losses.pytorch import MSE Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = RMoK(h=12, input_size=24, n_series=2, taylor_order=3, jacobi_degree=6, wavelet_function='mexican_hat', dropout=0.1, revin_affine=True, loss=MSE(), valid_loss=MAE(), early_stop_patience_steps=3, batch_size=32) fcst = NeuralForecast(models=[model], freq='ME') fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = fcst.predict(futr_df=Y_test_df) # Plot predictions fig, ax = plt.subplots(1, 1, figsize = (20, 7)) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['RMoK'], c='blue', label='Forecast') ax.set_title('AirPassengers Forecast', fontsize=22) ax.set_ylabel('Monthly Passengers', fontsize=20) ax.set_xlabel('Year', fontsize=20) ax.legend(prop={'size': 15}) ax.grid() ``` -------------------------------- ### MLPMultivariate Model Training and Prediction Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.mlpmultivariate.html This example demonstrates how to initialize, train, and predict using the `MLPMultivariate` model. It includes data preparation, model configuration, fitting, and plotting the forecasts. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import MLPMultivariate from neuralforecast.losses.pytorch import MAE from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = MLPMultivariate(h=12, input_size=24, n_series=2, stat_exog_list=['airline1'], futr_exog_list=['trend'], loss = MAE(), scaler_type='robust', learning_rate=1e-3, stat_exog_list=['airline1'], max_steps=200, val_check_steps=10, early_stop_patience_steps=2) fcst = NeuralForecast( models=[model], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = fcst.predict(futr_df=Y_test_df) # Plot predictions Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['MLPMultivariate'], c='blue', label='median') plt.grid() plt.legend() plt.plot() ``` -------------------------------- ### Install ML Libraries Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/comparing_methods.html Installs LightGBM and XGBoost, two popular gradient boosting libraries often used with MLForecast for time series modeling. ```python !pip install lightgbm xgboost ``` -------------------------------- ### TimeXer Model Training and Prediction Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.timexer.html This example demonstrates how to initialize, train, and use the TimeXer model for forecasting. It includes data preparation, model configuration, and plotting the predictions against the actual values. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import TimeXer from neuralforecast.losses.pytorch import MAE, MSE from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic, augment_calendar_df AirPassengersPanel, calendar_cols = augment_calendar_df(df=AirPassengersPanel, freq='M') Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = TimeXer(h=12, input_size=24, n_series=2, stat_exog_list=['airline1'], patch_len=12, hidden_size=128, n_heads=16, e_layers=2, d_ff=256, factor=1, dropout=0.1, use_norm=True, loss=MSE(), valid_loss=MAE(), early_stop_patience_steps=3, batch_size=32) fcst = NeuralForecast(models=[model], freq='ME') fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = fcst.predict(futr_df=Y_test_df) # Plot predictions fig, ax = plt.subplots(1, 1, figsize = (20, 7)) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['TimeXer'], c='blue', label='Forecast') ax.set_title('AirPassengers Forecast', fontsize=22) ax.set_ylabel('Monthly Passengers', fontsize=20) ax.set_xlabel('Year', fontsize=20) ax.legend(prop={'size': 15}) ax.grid() ``` -------------------------------- ### BiTCN Usage Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.bitcn.html Demonstrates how to use the BiTCN model for time series forecasting. This example includes data preparation, model initialization with specific parameters like input size, loss function, and exogenous variables, model fitting, prediction, and plotting of results including quantiles. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.losses.pytorch import GMM from neuralforecast.models import BiTCN from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test fcst = NeuralForecast( models=[ BiTCN(h=12, input_size=24, loss=GMM(n_components=7, level=[80,90]), max_steps=100, scaler_type='standard', futr_exog_list=['y_[lag12]'], hist_exog_list=None, stat_exog_list=['airline1'], windows_batch_size=2048, val_check_steps=10, early_stop_patience_steps=-1, ), ], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic) forecasts = fcst.predict(futr_df=Y_test_df) # Plot quantile predictions Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['BiTCN-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['BiTCN-lo-90'][-12:].values, y2=plot_df['BiTCN-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.legend() plt.grid() ``` -------------------------------- ### SOFTS Model Usage Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.softs.html Example demonstrating how to initialize, fit, and predict using the SOFTS model with the NeuralForecast framework. Includes data preparation and plotting of results. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import SOFTS from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic from neuralforecast.losses.pytorch import MASE Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = SOFTS(h=12, input_size=24, n_series=2, hidden_size=256, d_core=256, e_layers=2, d_ff=64, dropout=0.1, use_norm=True, loss=MASE(seasonality=4), early_stop_patience_steps=3, batch_size=32) fcst = NeuralForecast(models=[model], freq='ME') fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = fcst.predict(futr_df=Y_test_df) # Plot predictions fig, ax = plt.subplots(1, 1, figsize = (20, 7)) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['SOFTS'], c='blue', label='Forecast') ax.set_title('AirPassengers Forecast', fontsize=22) ax.set_ylabel('Monthly Passengers', fontsize=20) ax.set_xlabel('Year', fontsize=20) ax.legend(prop={'size': 15}) ax.grid() ``` -------------------------------- ### TCN Model Prediction and Visualization Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.tcn.html This example demonstrates how to initialize a TCN model, train it on historical data, make future predictions, and visualize the results along with confidence intervals. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import TCN from neuralforecast.losses.pytorch import DistributionLoss from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test fcst = NeuralForecast( models=[TCN(h=12, input_size=-1, loss=DistributionLoss(distribution='Normal', level=[80, 90]), learning_rate=5e-4, kernel_size=2, dilations=[1,2,4,8,16], encoder_hidden_size=128, context_size=10, decoder_hidden_size=128, decoder_layers=2, max_steps=500, scaler_type='robust', futr_exog_list=['y_[lag12]'], hist_exog_list=None, stat_exog_list=['airline1'], ) ], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic) forecasts = fcst.predict(futr_df=Y_test_df) # Plot quantile predictions Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['TCN-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['TCN-lo-90'][-12:].values, y2=plot_df['TCN-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.legend() plt.grid() plt.plot() ``` -------------------------------- ### Install NeuralForecast in Editable Mode (CUDA) Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/installation.html Install NeuralForecast in editable mode with the development dependencies and CUDA 11.8 PyTorch backend using 'uv'. ```bash $ uv pip install -e ".[dev]" --torch-backend cu118 ``` -------------------------------- ### XLinear Model Training and Prediction Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.xlinear.html This example demonstrates how to train and predict using the XLinear model. It includes data preparation, model initialization with various parameters, fitting the model to training data, and generating forecasts for test data. The snippet also includes plotting the predictions against the actual values. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import XLinear from neuralforecast.losses.pytorch import MAE from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = XLinear(h=12, input_size=24, n_series=2, stat_exog_list=['airline1'], hist_exog_list=["y_[lag12]"] futr_exog_list=['trend'], loss = MAE(), scaler_type='robust', learning_rate=1e-3, max_steps=200, val_check_steps=10, early_stop_patience_steps=2) fcst = NeuralForecast( models=[model], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = fcst.predict(futr_df=Y_test_df) # Plot predictions Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['XLinear'], c='blue', label='median') plt.grid() plt.legend() plt.plot() ``` -------------------------------- ### iTransformer Model Training and Prediction Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.itransformer.html This example demonstrates how to initialize an iTransformer model, train it using NeuralForecast, and then generate predictions on a test dataset. It also includes plotting the forecast against the actual values. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import iTransformer from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic from neuralforecast.losses.pytorch import MSE Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = iTransformer(h=12, input_size=24, n_series=2, hidden_size=128, n_heads=2, e_layers=2, d_layers=1, d_ff=4, factor=1, dropout=0.1, use_norm=True, loss=MSE(), valid_loss=MAE(), early_stop_patience_steps=3, batch_size=32, max_steps=100) fcst = NeuralForecast(models=[model], freq='ME') fcst.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = fcst.predict(futr_df=Y_test_df) # Plot predictions fig, ax = plt.subplots(1, 1, figsize = (20, 7)) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['iTransformer'], c='blue', label='Forecast') ax.set_title('AirPassengers Forecast', fontsize=22) ax.set_ylabel('Monthly Passengers', fontsize=20) ax.set_xlabel('Year', fontsize=20) ax.legend(prop={'size': 15}) ax.grid() ``` -------------------------------- ### BaseAuto Initialization and Fit with Optuna Source: https://nixtlaverse.nixtla.io/neuralforecast/common.base_auto.html Example of initializing BaseAuto with an Optuna configuration function and sampler, then fitting it to a dataset. It verifies the results object type and prediction accuracy. ```python auto2 = BaseAuto(h=12, loss=MAE(), valid_loss=MSE(), cls_model=MLP, config=config_f, search_alg=optuna.samplers.RandomSampler(), num_samples=2, backend='optuna', callbacks=[OptunaLogLossesCallback()]) auto2.fit(dataset=dataset) assert isinstance(auto2.results, optuna.Study) y_hat2 = auto2.predict(dataset=dataset) assert mae(Y_test_df['y'].values, y_hat2[:, 0]) < 200 ``` -------------------------------- ### BaseAuto Initialization and Fit with Ray Tune Source: https://nixtlaverse.nixtla.io/neuralforecast/common.base_auto.html Example of initializing BaseAuto with a configuration for Ray Tune and fitting it to a dataset. It includes a custom callback for logging. ```python config = { "hidden_size": tune.choice([512]), "num_layers": tune.choice([3, 4]), "input_size": 12, "max_steps": 10, "val_check_steps": 5 } auto = BaseAuto(h=12, loss=MAE(), valid_loss=MSE(), cls_model=MLP, config=config, num_samples=2, cpus=1, gpus=0, callbacks=[RayLogLossesCallback()]) auto.fit(dataset=dataset) y_hat = auto.predict(dataset=dataset) assert mae(Y_test_df['y'].values, y_hat[:, 0]) < 200 ``` -------------------------------- ### Setup Imports and Warnings Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/weighting_timesteps.html Imports necessary libraries for NeuralForecast, data manipulation, evaluation, and plotting. It also configures warnings and logging levels. ```python import logging import warnings import numpy as np from utilsforecast.evaluation import evaluate from utilsforecast.losses import mae from utilsforecast.plotting import plot_series from neuralforecast import NeuralForecast from neuralforecast.models import NHITS warnings.filterwarnings("ignore") logging.getLogger("pytorch_lightning").setLevel(logging.ERROR) ``` -------------------------------- ### Install NeuralForecast with Conda Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/installation.html Use this command to install the released version of NeuralForecast from the conda-forge channel. ```shell conda install -c conda-forge neuralforecast ``` -------------------------------- ### Install Seaborn for Visualization Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/comparing_methods.html Installs the seaborn library, which is used for creating statistical visualizations, including error distributions. ```python %%capture !pip install seaborn ``` -------------------------------- ### Configure HINT Model Parameters Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/hierarchical_forecasting.html Sets up the HINT model, including the forecast horizon, input size, loss function (GMM with quantiles), exogenous variables, and training parameters like max steps and early stopping. It also defines the base NHITS model and the reconciliation strategy. ```python # Horizon and quantiles level = np.arange(0, 100, 2) qs = [[50-lv/2, 50+lv/2] if lv!=0 else [50] for lv in level] quantiles = np.sort(np.concatenate(qs)/100) # HINT := BaseNetwork + Distribution + Reconciliation nhits = NHITS(h=horizon, input_size=24, loss=GMM(n_components=10, quantiles=quantiles), hist_exog_list=['month'], max_steps=2000, early_stop_patience_steps=10, val_check_steps=50, scaler_type='robust', learning_rate=1e-3, valid_loss=sCRPS(quantiles=quantiles)) model = HINT(h=horizon, S=S_df.drop(columns='unique_id').values, model=nhits, reconciliation='BottomUp') ``` -------------------------------- ### Install Packages Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/tutorials/hierarchical_forecasting.html Installs the required packages for hierarchical forecasting: datasetsforecast, hierarchicalforecast, neuralforecast, and statsforecast. This command is typically run in a notebook environment. ```python %%capture !pip install datasetsforecast hierarchicalforecast neuralforecast statsforecast ``` -------------------------------- ### Prepare Data and Initialize Models Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/getting-started/introduction.html Load the AirPassengers dataset, split it into training and testing sets, and initialize NBEATS and NHITS models with specified parameters. ```python # Split data and declare panel dataset Y_df = AirPassengersDF Y_train_df = Y_df[Y_df.ds<='1959-12-31'] # 132 train Y_test_df = Y_df[Y_df.ds>'1959-12-31'] # 12 test # Fit and predict with NBEATS and NHITS models horizon = len(Y_test_df) models = [NBEATS(input_size=2 * horizon, h=horizon, max_steps=100, enable_progress_bar=False), NHITS(input_size=2 * horizon, h=horizon, max_steps=100, enable_progress_bar=False)] nf = NeuralForecast(models=models, freq='ME') nf.fit(df=Y_train_df) Y_hat_df = nf.predict() ``` -------------------------------- ### DilatedRNN Usage Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.dilated_rnn.html Demonstrates how to use the DilatedRNN model for time series forecasting. This example includes data preparation, model initialization, training, prediction, and visualization of results. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import DilatedRNN from neuralforecast.losses.pytorch import DistributionLoss from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test fcst = NeuralForecast( models=[DilatedRNN(h=12, input_size=-1, loss=DistributionLoss(distribution='Normal', level=[80, 90]), scaler_type='robust', encoder_hidden_size=100, max_steps=200, futr_exog_list=['y_[lag12]'], hist_exog_list=None, stat_exog_list=['airline1'], ) ], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic) forecasts = fcst.predict(futr_df=Y_test_df) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['DilatedRNN-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['DilatedRNN-lo-90'][-12:].values, y2=plot_df['DilatedRNN-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.legend() plt.grid() plt.plot() ``` -------------------------------- ### Instantiate BiTCN Model Source: https://nixtlaverse.nixtla.io/neuralforecast/models.bitcn.html This snippet shows how to instantiate the BiTCN model with its required and optional parameters. Ensure you provide the forecast horizon (h) and input size (input_size). ```python BiTCN( h, input_size, hidden_size=16, dropout=0.5, futr_exog_list=None, hist_exog_list=None, stat_exog_list=None, exclude_insample_y=False, loss=MAE(), valid_loss=None, max_steps=1000, learning_rate=0.001, num_lr_decays=-1, early_stop_patience_steps=-1, val_monitor="ptl/val_loss", val_check_steps=100, batch_size=32, valid_batch_size=None, windows_batch_size=1024, inference_windows_batch_size=1024, start_padding_enabled=False, training_data_availability_threshold=0.0, step_size=1, scaler_type="identity", random_seed=1, drop_last_loader=False, alias=None, optimizer=None, optimizer_kwargs=None, lr_scheduler=None, lr_scheduler_kwargs=None, dataloader_kwargs=None, **trainer_kwargs ) ``` -------------------------------- ### DeepNPTS Usage Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.deepnpts.html Example demonstrating how to use the DeepNPTS model for time series forecasting. It includes data preparation, model initialization, fitting, prediction, and plotting the results. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import DeepNPTS from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test nf = NeuralForecast( models=[DeepNPTS(h=12, input_size=24, stat_exog_list=['airline1'], futr_exog_list=['trend'], max_steps=1000, val_check_steps=10, early_stop_patience_steps=3, scaler_type='robust', enable_progress_bar=True), ], freq='ME' ) nf.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) Y_hat_df = nf.predict(futr_df=Y_test_df) # Plot quantile predictions Y_hat_df = Y_hat_df.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['DeepNPTS'], c='red', label='mean') plt.grid() plt.plot() ``` -------------------------------- ### Import Optuna Logging Source: https://nixtlaverse.nixtla.io/neuralforecast/docs/capabilities/hyperparameter_tuning.html Import the Optuna library and set logging verbosity to WARNING to suppress training output. ```python import optuna ``` -------------------------------- ### HINT Model Initialization Source: https://nixtlaverse.nixtla.io/neuralforecast/models.hint.html Initializes the HINT model with forecast horizon, model architecture, summing matrix, and reconciliation method. Available reconciliation methods include 'BottomUp', 'MinTraceOLS', and 'MinTraceWLS'. ```python HINT(h, S, model, reconciliation, alias=None) ``` -------------------------------- ### KAN Model Usage Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.kan.html This example demonstrates how to use the KAN model for time series forecasting. It includes data preparation, model initialization, fitting, prediction, and plotting the results. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import KAN from neuralforecast.losses.pytorch import DistributionLoss from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test fcst = NeuralForecast( models=[ KAN(h=12, input_size=24, loss = DistributionLoss(distribution="Normal"), max_steps=100, scaler_type='standard', futr_exog_list=['y_[lag12]'], hist_exog_list=None, stat_exog_list=['airline1'], ), ], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic) forecasts = fcst.predict(futr_df=Y_test_df) # Plot quantile predictions Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['KAN-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['KAN-lo-90'][-12:].values, y2=plot_df['KAN-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.legend() plt.grid() ``` -------------------------------- ### PatchTST Model Training and Forecasting Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.patchtst.html Demonstrates a complete workflow for training a PatchTST model, generating forecasts, and visualizing the results using the AirPassengers dataset. Includes data preparation, model instantiation with distribution loss, fitting, prediction, and plotting. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import PatchTST from neuralforecast.losses.pytorch import DistributionLoss from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic, augment_calendar_df AirPassengersPanel, calendar_cols = augment_calendar_df(df=AirPassengersPanel, freq='M') Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test model = PatchTST(h=12, input_size=104, patch_len=24, stride=24, revin=False, hidden_size=16, n_heads=4, scaler_type='robust', loss=DistributionLoss(distribution='StudentT', level=[80, 90]), learning_rate=1e-3, max_steps=500, val_check_steps=50, early_stop_patience_steps=2) nf = NeuralForecast( models=[model], freq='ME' ) nf.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12) forecasts = nf.predict(futr_df=Y_test_df) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) if model.loss.is_distribution_output: plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['PatchTST-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['PatchTST-lo-90'][-12:].values, y2=plot_df['PatchTST-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.grid() plt.legend() plt.plot() else: plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['PatchTST'], c='blue', label='Forecast') plt.legend() plt.grid() ``` -------------------------------- ### GRU Model Usage Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.gru.html Example demonstrating how to use the GRU model for time series forecasting. It includes data loading, model initialization, fitting, prediction, and plotting the results. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast # from neuralforecast.models import GRU from neuralforecast.losses.pytorch import DistributionLoss from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test fcst = NeuralForecast( models=[GRU(h=12, input_size=24, loss=DistributionLoss(distribution='Normal', level=[80, 90]), scaler_type='robust', encoder_n_layers=2, encoder_hidden_size=128, decoder_hidden_size=128, decoder_layers=2, max_steps=200, futr_exog_list=None, hist_exog_list=['y_[lag12]'], stat_exog_list=['airline1'], ) ], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic) forecasts = fcst.predict(futr_df=Y_test_df) Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['GRU-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['GRU-lo-90'][-12:].values, y2=plot_df['GRU-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.legend() plt.grid() plt.plot() ``` -------------------------------- ### TiDE Model Prediction and Plotting Example Source: https://nixtlaverse.nixtla.io/neuralforecast/models.tide.html Demonstrates how to fit a TiDE model and generate predictions using sample data. Includes plotting the forecasted values against the true values and confidence intervals. ```python import pandas as pd import matplotlib.pyplot as plt from neuralforecast import NeuralForecast from neuralforecast.models import TiDE from neuralforecast.losses.pytorch import GMM from neuralforecast.utils import AirPassengersPanel, AirPassengersStatic Y_train_df = AirPassengersPanel[AirPassengersPanel.ds=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test fcst = NeuralForecast( models=[ TiDE(h=12, input_size=24, loss=GMM(n_components=7, return_params=True, level=[80,90], weighted=True), max_steps=100, scaler_type='standard', futr_exog_list=['y_[lag12]'], hist_exog_list=None, stat_exog_list=['airline1'], ), ], freq='ME' ) fcst.fit(df=Y_train_df, static_df=AirPassengersStatic) forecasts = fcst.predict(futr_df=Y_test_df) # Plot quantile predictions Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds']) plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1) plot_df = pd.concat([Y_train_df, plot_df]) plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1) plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True') plt.plot(plot_df['ds'], plot_df['TiDE-median'], c='blue', label='median') plt.fill_between(x=plot_df['ds'][-12:], y1=plot_df['TiDE-lo-90'][-12:].values, y2=plot_df['TiDE-hi-90'][-12:].values, alpha=0.4, label='level 90') plt.legend() plt.grid() ```