https://github.com/nixtla logo
#neural-forecast
Title
# neural-forecast
a

Akmal Soliev

11/20/2023, 4:42 PM
Hello, I am trying to predict values of unseen data based on the historical data of a similar products. Example: I'm trying to predict the sales of a new shirt that will be released in summer, I already have data from the sales of a similar type of shirts in previous year(s)/month(s). Based on the guidance by Kin it appears that this can be achieved using Temporal Fusion Transformer (https://nixtla.mintlify.app/neuralforecast/models.tft.html), however, I am struggling in its application. ā€¢ My assumption is that
unique_id
does not play any role in the following model and can be simply set as
"shirts"
, with overlapping/repeating dates (
ds
) from previous shirt sales? NOTE: There are exogenous variables available (for the sake of an example), such as weather, humidity and number of people in the city. Example application using AirPassenger dataset:
Copy code
import pandas as pd
import pytorch_lightning as pl
import matplotlib.pyplot as plt

from neuralforecast import NeuralForecast
#from neuralforecast.models import TFT
from neuralforecast.losses.pytorch import MQLoss, DistributionLoss, GMM, PMM
from neuralforecast.tsdataset import TimeSeriesDataset
from neuralforecast.utils import AirPassengers, AirPassengersPanel, AirPassengersStatic
Copy code
#AirPassengersPanel['y'] = AirPassengersPanel['y'] + 10
Y_train_df = AirPassengersPanel[AirPassengersPanel.ds<AirPassengersPanel['ds'].values[-12]] # 132 train
Y_test_df = AirPassengersPanel[AirPassengersPanel.ds>=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True)
Modification:
Copy code
Y_train_df["unique_id"] = "Airline1"
Y_test_df = Y_test_df[lambda x: x["unique_id"] == "Airline1"]
Modeling:
Copy code
nf = NeuralForecast(
    models=[TFT(h=12, input_size=48,
                hidden_size=20,
                #loss=DistributionLoss(distribution='Poisson', level=[80, 90]),
                #loss=DistributionLoss(distribution='Normal', level=[80, 90]),
                loss=DistributionLoss(distribution='StudentT', level=[80, 90]),
                learning_rate=0.005,
                stat_exog_list=['airline1'],
                #futr_exog_list=['y_[lag12]'],
                hist_exog_list=['trend'],
                max_steps=500,
                val_check_steps=10,
                early_stop_patience_steps=10,
                scaler_type='robust',
                windows_batch_size=None,
                enable_progress_bar=True),
    ],
    freq='M'
)
nf.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12)
Y_hat_df = nf.predict(futr_df=Y_test_df)
c

Cristian (Nixtla)

11/21/2023, 4:25 PM
Hi @Akmal Soliev šŸ™‚. Yes, it should be possible to use the sales of a different
unique_id
to predict a new article (in fact, any model would work). However, the change should only be done during the
predict
step. My recommendations are: 1. Fit your model on the historical values of your existing shirts, as you would normally do. There is no need to change any
unique_id
. Do not change and group the different products in the same
unique_id
. Each
unique_id
should only have one value for each
ds
. 2. When using the
predict
method, pass a new
df
using the appropriate values for
y
from a different article. For instance, set the dates for the current year but take the values for
y
of the existing similar shirts from the previous year. (Of course, this approach is making many assumptions to work properly). Let me know if this is clear.
a

Akmal Soliev

11/21/2023, 4:31 PM
Yes, it should be possible to use the sales of a different
unique_id
to predict a new article
In this case if I have passed
blue shirt
red shirt
white shirt
as train unique id I'd still be able to predict
yellow shirt
? I think if there was a minimal example that would help a ton! Thanks Christian, struggling to wrap my head around this idea.
c

Cristian (Nixtla)

11/21/2023, 4:39 PM
With neuralforecast models you can use a trained model to forecast any time series you want by simply passing a new
df
in the
predict
method. The
unique_id
of the
df
does not have to be contained in the train data. By training on
blue shirt
red shirt
white shirt
, etc. the model has learned to forecast multiple types of shirts. You can then construct your
df
for the predict method with a new
unique_id
, for example
yellow_shirt
, and take the
y
values of a "very similar" shirt. Or maybe produce multiple forecasts for several "very similar" shirts and then take the average/mean.
This related to transfer learning (for example here: https://nixtla.mintlify.app/neuralforecast/examples/transfer_learning.html). Note how we forecast AirPassengers data with a model trained on M4 by passing the
df
dataset in the
predict
method. Its the same principle
a

Akmal Soliev

11/21/2023, 5:14 PM
1. When using the
predict
method, pass a new
df
using the appropriate values for
y
from a different article. For instance, set the dates for the current year but take the values for
y
of the existing similar shirts from the previous year. (Of course, this approach is making many assumptions to work properly).
okay i literally just had to change the parameter šŸ¤¦, was so confused on why you said
df
šŸ˜„ Does it work with exo variables? I attempted to run this on Airpassanger example from above with the following variation to
Y_test_df
:
Copy code
unique_id	    ds	        y	  trend	y_[lag12]
0	TestingAirlines	1960-01-31	417.0	132	360.0
1	TestingAirlines	1960-02-29	391.0	133	342.0
2	TestingAirlines	1960-03-31	419.0	134	406.0
3	TestingAirlines	1960-04-30	461.0	135	396.0
4	TestingAirlines	1960-05-31	472.0	136	420.0
I am getting the following error:
Copy code
Exception: {'airline1'} static exogenous variables not found in input dataset
c

Cristian (Nixtla)

11/21/2023, 6:48 PM
Yes, it allows for exogenous. In the dataframe for the predict method you must include all variables added when taining
a

Akmal Soliev

11/21/2023, 8:29 PM
all features? If yes, then all features are identical. The only difference is the
unique_id
Untitled.ipynb
c

Cristian (Nixtla)

11/21/2023, 10:22 PM
Yes, if you pass all the same features then the forecasts will be the same. You essentially assume, for example, the forecasts of the
yellow_shirt
will be identical to the
blue_shirt
. However, depending on your exogenous features you might have different behaviour (such as weather or number of people for the new year). Another option as I mentioned is to take some form of aggregation from multiple forecasts for different similar products (it has a k-nearest neighbor flavor).
a

Akmal Soliev

11/22/2023, 11:50 AM
I have attempted the first option, but it gives me an error :/ Second option is nice, will look into it after the first one
c

Cristian (Nixtla)

11/23/2023, 7:15 PM
One last option is to leverage TFT's static_features in the encoder directly. You should first create several static features to characterize products (for example category, color, size, etc.). Then use static features + calendar variables to produce forecasts without past history.
3 Views