From ad840deebbd8682318e93aa3232ae97658b9c9f2 Mon Sep 17 00:00:00 2001 From: geetu040 Date: Fri, 1 Mar 2024 08:24:08 +0500 Subject: [PATCH] [ENH] `NeuralForecastRNN` should auto-detect `freq` (#6003) Enhances `NeuralForecastRNN` to interpret `freq` from `ForecastingHorizon` when passed as `"auto"` --- .all-contributorsrc | 9 +++++++++ .../forecasting/base/adapters/_neuralforecast.py | 16 ++++++++++++++-- sktime/forecasting/neuralforecast.py | 6 ++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 0f7a286d3e4..a3da25d70df 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -2604,6 +2604,15 @@ "bug", "code" ] + }, + { + "login": "geetu040", + "name": "Armaghan", + "avatar_url": "https://avatars.githubusercontent.com/u/90601662?s=96&v=4", + "profile": "https://github.com/geetu040", + "contributions": [ + "code" + ] } ] } diff --git a/sktime/forecasting/base/adapters/_neuralforecast.py b/sktime/forecasting/base/adapters/_neuralforecast.py index dcf09acc54a..80583ce574d 100644 --- a/sktime/forecasting/base/adapters/_neuralforecast.py +++ b/sktime/forecasting/base/adapters/_neuralforecast.py @@ -17,8 +17,10 @@ class _NeuralForecastAdapter(BaseForecaster): Parameters ---------- - freq : str + freq : str (default="auto") frequency of the data, see available frequencies [1]_ from ``pandas`` + + default ("auto") interprets freq from ForecastingHorizon in ``fit`` local_scaler_type : str (default=None) scaler to apply per-series to all features before fitting, which is inverted after predicting @@ -66,7 +68,7 @@ class _NeuralForecastAdapter(BaseForecaster): def __init__( self: "_NeuralForecastAdapter", - freq: str, + freq: str = "auto", local_scaler_type: typing.Optional[ typing.Literal["standard", "robust", "robust-iqr", "minmax", "boxcox"] ] = None, @@ -179,6 +181,16 @@ def _fit( if not fh.is_all_out_of_sample(cutoff=self.cutoff): raise NotImplementedError("in-sample prediction is currently not supported") + if self.freq == "auto": + if fh.freq: + # interpret freq from ForecastingHorizon + self.freq = fh.freq + else: + # when freq is not interpreted from ForecastingHorizon + raise ValueError( + "Could not interpret freq, try passing freq in model initialization" + ) + train_indices = y.index if isinstance(train_indices, pandas.PeriodIndex): train_indices = train_indices.to_timestamp(freq=self.freq) diff --git a/sktime/forecasting/neuralforecast.py b/sktime/forecasting/neuralforecast.py index 8d2c452a0d0..0c291a1518b 100644 --- a/sktime/forecasting/neuralforecast.py +++ b/sktime/forecasting/neuralforecast.py @@ -22,8 +22,10 @@ class NeuralForecastRNN(_NeuralForecastAdapter): Parameters ---------- - freq : str + freq : str (default="auto") frequency of the data, see available frequencies [4]_ from ``pandas`` + + default ("auto") interprets freq from ForecastingHorizon in ``fit`` local_scaler_type : str (default=None) scaler to apply per-series to all features before fitting, which is inverted after predicting @@ -156,7 +158,7 @@ class NeuralForecastRNN(_NeuralForecastAdapter): def __init__( self: "NeuralForecastRNN", - freq: str, + freq: str = "auto", local_scaler_type: typing.Optional[ typing.Literal["standard", "robust", "robust-iqr", "minmax", "boxcox"] ] = None,