Core
Data format
The required input format is a dataframe with at least the following
columns: * unique_id
with a unique identifier for each time serie *
ds
with the datestamp and a column * y
with the values of the
serie.
Every other column is considered a static feature unless stated
otherwise in TimeSeries.fit
unique_id | ds | y | static_0 | static_1 | |
---|---|---|---|---|---|
0 | id_00 | 2000-01-01 | 7.404529 | 27 | 53 |
1 | id_00 | 2000-01-02 | 35.952624 | 27 | 53 |
2 | id_00 | 2000-01-03 | 68.958353 | 27 | 53 |
3 | id_00 | 2000-01-04 | 84.994505 | 27 | 53 |
4 | id_00 | 2000-01-05 | 113.219810 | 27 | 53 |
… | … | … | … | … | … |
4869 | id_19 | 2000-03-25 | 400.606807 | 97 | 45 |
4870 | id_19 | 2000-03-26 | 538.794824 | 97 | 45 |
4871 | id_19 | 2000-03-27 | 620.202104 | 97 | 45 |
4872 | id_19 | 2000-03-28 | 20.625426 | 97 | 45 |
4873 | id_19 | 2000-03-29 | 141.513169 | 97 | 45 |
For simplicity we’ll just take one time serie here.
unique_id | ds | y | static_0 | static_1 | |
---|---|---|---|---|---|
0 | id_00 | 2000-01-01 | 7.404529 | 27 | 53 |
1 | id_00 | 2000-01-02 | 35.952624 | 27 | 53 |
2 | id_00 | 2000-01-03 | 68.958353 | 27 | 53 |
3 | id_00 | 2000-01-04 | 84.994505 | 27 | 53 |
4 | id_00 | 2000-01-05 | 113.219810 | 27 | 53 |
… | … | … | … | … | … |
217 | id_00 | 2000-08-05 | 13.263188 | 27 | 53 |
218 | id_00 | 2000-08-06 | 38.231981 | 27 | 53 |
219 | id_00 | 2000-08-07 | 59.555183 | 27 | 53 |
220 | id_00 | 2000-08-08 | 86.986368 | 27 | 53 |
221 | id_00 | 2000-08-09 | 119.254810 | 27 | 53 |
source
TimeSeries
Utility class for storing and transforming time series data.
The
TimeSeries
class takes care of defining the transformations to be performed
(lags
, lag_transforms
and date_features
). The transformations can
be computed using multithreading if num_threads > 1
.
The frequency is converted to an offset.
The date features are stored as they were passed to the constructor.
The transformations are stored as a dictionary where the key is the name
of the transformation (name of the column in the dataframe with the
computed features), which is built using build_transform_name
and the
value is a tuple where the first element is the lag it is applied to,
then the function and then the function arguments.
Note that for lags
we define the transformation as the identity
function applied to its corresponding lag. This is because
_transform_series
takes the lag as an argument and shifts the array before computing the
transformation.
source
TimeSeries.fit_transform
*Add the features to data
and save the required information for the
predictions step.
If not all features are static, specify which ones are in
static_features
. If you don’t want to drop rows with null values after
the transformations set dropna=False
If keep_last_n
is not None then
that number of observations is kept across all series for updates.*
The series values are stored as a GroupedArray in an attribute ga
. If
the data type of the series values is an int then it is converted to
np.float32
, this is because lags generate np.nan
s so we need a float
data type for them.
The series ids are stored in an uids
attribute.
For each time serie, the last observed date is stored so that predictions start from the last date + the frequency.
The last row of every serie without the y
and ds
columns are taken
as static features.
If you pass static_features
to
TimeSeries.fit_transform
then only these are kept.
You can also specify keep_last_n in TimeSeries.fit_transform, which means that after computing the features for training we want to keep only the last n samples of each time serie for computing the updates. This saves both memory and time, since the updates are performed by running the transformation functions on all time series again and keeping only the last value (the update).
If you have very long time series and your updates only require a small sample it’s recommended that you set keep_last_n to the minimum number of samples required to compute the updates, which in this case is 15 since we have a rolling mean of size 14 over the lag 2 and in the first update the lag 2 becomes the lag 1. This is because in the first update the lag 1 is the last value of the series (or the lag 0), the lag 2 is the lag 1 and so on.
TimeSeries.fit_transform
requires that the y column doesn’t have any null values. This is
because the transformations could propagate them forward, so if you have
null values in the y column you’ll get an error.
source
TimeSeries.predict
Once we have a trained model we can use
TimeSeries.predict
passing the model and the horizon to get the predictions back.
If we have dynamic features we can pass them to X_df
.
source
TimeSeries.update
Update the values of the stored series.