unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
8.11k stars 884 forks source link

Error in Historical Forecast Using Static Covariates Trained by fit_from_dataset #2110

Open gitbooo opened 11 months ago

gitbooo commented 11 months ago

Issue description

I am encountering an error when attempting to perform a historical forecast using the DLinear model. The specific error message is: This model has been trained with static_covariates; some static_covariates of matching dimensionality are needed for prediction.

Context:

To Reproduce

future_covs_series = [series.astype(np.float32) for series in TimeSeries.from_group_dataframe(future_covariates, group_cols='Name')]
past_covs_series = [series.astype(np.float32) for series in TimeSeries.from_group_dataframe(past_covariates, group_cols='Name')]
target= [series.astype(np.float32) for series in TimeSeries.from_group_dataframe(target, group_cols='Name')]

ds_train = MixedCovariatesSequentialDataset(target_series=train_target , past_covariates=train_past_covs,
                                                future_covariates=train_future_covs,
                                                input_chunk_length=input_chunk_length, 
                                                output_chunk_length=output_chunk_length, max_samples_per_ts=None,
                                                use_static_covariates=True)
ds_val = MixedCovariatesSequentialDataset(target_series=val_target, past_covariates=val_past_covs,
                                                future_covariates=val_future_covs,
                                                input_chunk_length=input_chunk_length, 
                                                output_chunk_length=output_chunk_length, max_samples_per_ts=None,
                                                use_static_covariates=True)

model = DLinearModel(input_chunk_length=input_chunk_length, output_chunk_length=output_chunk_length, use_static_covariates=True)

forecasts=model.historical_forecasts(series=test_targets,
                           future_covariates=test_future,
                            past_covariates=test_past_covs,
                            forecast_horizon=output_chunk_length,
                            verbose=True,
                            last_points_only=False,
                            stride=output_chunk_length,
                            retrain=False)

Current Observation:

Expected Behavior:

The model should be able to perform historical forecasting with static covariates without encountering dimensionality issues, given that the training was successful with these settings.

System:

Thank you in advance for any assistance or insights provided!

madtoinou commented 11 months ago

Hi @gitbooo,

The error message seems to indicate that test_targets does not contain the same number of static covariates as the series used during training.

In order to use your code snippet, we would also need an example of the data you are using. You can generate dummy time series using the functions in the darts.utils.timeseries_generation module (and make sure to add the static covariates where it's relevant).

Can you check what is contained in the model._expect_static_covariates and model._uses_static_covariates attributes of the model?