def test_minute_frequency():
    prediction_length = 1
    timeseries = {
        TIMESERIES_KEYS.START: "2021-01-15 12:40:00",
        TIMESERIES_KEYS.TARGET: np.array([12, 13]),
        TIMESERIES_KEYS.TARGET_NAME: "target",
        TIMESERIES_KEYS.TIME_COLUMN_NAME: "date",
    }
    frequency = "20min"
    gluon_dataset = ListDataset([timeseries], freq=frequency)
    model = Model(
        "simplefeedforward",
        model_parameters={"activated": True, "kwargs": {}},
        frequency=frequency,
        prediction_length=prediction_length,
        epoch=1,
        batch_size=8,
        num_batches_per_epoch=5,
    )
    evaluation_forecasts_df = model.train_evaluate(gluon_dataset, gluon_dataset, make_forecasts=True, retrain=True)[2]
    assert evaluation_forecasts_df["index"].iloc[0] == pd.Timestamp("2021-01-15 13:00:00")

    trained_model = TrainedModel(
        predictor=model.predictor,
        gluon_dataset=gluon_dataset,
        prediction_length=prediction_length,
        quantiles=[0.5],
        include_history=True,
    )
    trained_model.predict()
    forecasts_df = trained_model.get_forecasts_df(session="2021-01-01", model_label="TEST")
    assert forecasts_df["date"].iloc[0] == pd.Timestamp("2021-01-15 13:20:00")
Exemplo n.º 2
0
 def setup_method(self):
     self.session_name = datetime.utcnow().isoformat() + "Z"
     self.model_label = "TrivialIdentity"
     self.trained_model = TrainedModel(
         predictor=self.predictor,
         gluon_dataset=self.gluon_dataset,
         prediction_length=self.prediction_length,
         quantiles=[0.1, 0.5, 0.9],
         include_history=True,
     )
    def test_simplefeedforward_external_features_predictions(self):
        external_features_future_df = pd.DataFrame(
            {
                "date": ["2018-01-10", "2018-01-11", "2018-01-13", "2018-01-14"],
                "key": [1, 1, 2, 2],
                "ext_feat": [0, 0, 0, 1],
            }
        )
        external_features_future_df["date"] = pd.to_datetime(external_features_future_df["date"]).dt.tz_localize(tz=None)

        gluon_dataset = add_future_external_features(self.test_list_dataset, external_features_future_df, self.prediction_length, self.frequency)

        trained_model = TrainedModel(
            model_name=self.model_name,
            predictor=self.model.predictor,
            gluon_dataset=gluon_dataset,
            prediction_length=self.prediction_length,
            quantiles=[0.1, 0.5, 0.9],
            include_history=True,
        )
        trained_model.predict()
        forecasts_df = trained_model.get_forecasts_df(session="1234")
        assert len(forecasts_df.index) == 13
gluon_train_dataset = model_selection.get_gluon_train_dataset()

external_features = external_features_check(
    gluon_train_dataset, params["external_features_future_dataset"])

if external_features:
    external_features_future_df = params[
        "external_features_future_dataset"].get_dataframe()
    gluon_train_dataset = add_future_external_features(
        gluon_train_dataset, external_features_future_df,
        predictor.prediction_length, predictor.freq)

trained_model = TrainedModel(
    predictor=predictor,
    gluon_dataset=gluon_train_dataset,
    prediction_length=params["prediction_length"],
    quantiles=params["quantiles"],
    include_history=params["include_history"],
    history_length_limit=params["history_length_limit"],
)

trained_model.predict()

logger.info(
    "Forecasting future values: Done in {:.2f} seconds".format(perf_counter() -
                                                               start))

forecasts_df = trained_model.get_forecasts_df(
    session=model_selection.get_session_name(),
    model_label=model_selection.get_model_label())
params["output_dataset"].write_with_schema(forecasts_df)
Exemplo n.º 5
0
class TestTrainedModel:
    def setup_class(self):
        self.frequency = "D"
        self.prediction_length = 2
        timeseries_0 = {
            TIMESERIES_KEYS.START:
            "2018-01-01",
            TIMESERIES_KEYS.TARGET:
            np.array([12, 13, 14, 15]),
            TIMESERIES_KEYS.TARGET_NAME:
            "sales",
            TIMESERIES_KEYS.TIME_COLUMN_NAME:
            "date",
            TIMESERIES_KEYS.FEAT_DYNAMIC_REAL:
            np.array([[1, 0, 0, 0, 0, 1], [0, 0, 0, 1, 1, 0]]),
            TIMESERIES_KEYS.FEAT_DYNAMIC_REAL_COLUMNS_NAMES:
            ["is_holiday", "is_weekend"],
            TIMESERIES_KEYS.IDENTIFIERS: {
                "store": 1,
                "item": 1
            },
        }
        timeseries_1 = {
            TIMESERIES_KEYS.START:
            "2018-01-01",
            TIMESERIES_KEYS.TARGET:
            np.array([2, 3, 4, 5]),
            TIMESERIES_KEYS.TARGET_NAME:
            "sales",
            TIMESERIES_KEYS.TIME_COLUMN_NAME:
            "date",
            TIMESERIES_KEYS.FEAT_DYNAMIC_REAL:
            np.array([[1, 0, 0, 0, 0, 1], [0, 0, 0, 1, 1, 0]]),
            TIMESERIES_KEYS.FEAT_DYNAMIC_REAL_COLUMNS_NAMES:
            ["is_holiday", "is_weekend"],
            TIMESERIES_KEYS.IDENTIFIERS: {
                "store": 1,
                "item": 2
            },
        }
        self.gluon_dataset = ListDataset([timeseries_0, timeseries_1],
                                         freq=self.frequency)
        self.predictor = IdentityPredictor(prediction_length=2,
                                           freq="D",
                                           num_samples=100)

    def setup_method(self):
        self.session_name = datetime.utcnow().isoformat() + "Z"
        self.model_label = "TrivialIdentity"
        self.trained_model = TrainedModel(
            model_name="trivial_identity",
            predictor=self.predictor,
            gluon_dataset=self.gluon_dataset,
            prediction_length=self.prediction_length,
            quantiles=[0.1, 0.5, 0.9],
            include_history=True,
        )

    def test_predict(self):
        self.trained_model.predict()
        forecasts_df = self.trained_model.get_forecasts_df(
            session=self.session_name)
        expected_forecasts_columns = [
            "date",
            "store",
            "item",
            "is_holiday",
            "is_weekend",
            "sales",
            "forecast_lower_sales",
            "forecast_sales",
            "forecast_upper_sales",
            METRICS_DATASET.SESSION,
            METRICS_DATASET.MODEL_COLUMN,
            ROW_ORIGIN.COLUMN_NAME,
        ]
        assert set(expected_forecasts_columns) == set(forecasts_df.columns)

        future_df = forecasts_df[forecasts_df["item"] == 2].iloc[0:2]
        history_df = forecasts_df[forecasts_df["item"] == 2].iloc[2:]

        assert future_df["sales"].count() == 0 and history_df["sales"].count(
        ) == 4
        assert future_df["forecast_sales"].count(
        ) == 2 and history_df["forecast_sales"].count() == 0