コード例 #1
0
 def build_from_ckpt(self, checkpoint_filename):
     """
     Build Prophet Model from checkpoint
     :param checkpoint_filename: model checkpoint filename
     """
     from zoo.chronos.model.prophet import ProphetModel
     model = ProphetModel(config=self.model_config)
     model.restore(checkpoint_filename)
     return model
コード例 #2
0
    def test_save_restore(self):
        self.model.fit_eval(data=self.data, **self.config)
        result_save = self.model.predict(x=None, horizon=self.horizon)
        model_file = "tmp.json"

        self.model.save(model_file)
        assert os.path.isfile(model_file)
        new_model = ProphetModel()
        new_model.restore(model_file)
        assert new_model.model
        result_restore = new_model.predict(horizon=self.horizon)
        assert_array_almost_equal(result_save['yhat'], result_restore['yhat'], decimal=2), \
            "Prediction values are not the same after restore: " \
            "predict before is {}, and predict after is {}".format(result_save, result_restore)
        os.remove(model_file)
コード例 #3
0
class ProphetForecaster(Forecaster):
    """
    Example:
        >>> #The dataset is split into data, validation_data
        >>> model = ProphetForecaster(changepoint_prior_scale=0.05, seasonality_mode='additive')
        >>> model.fit(data, validation_data)
        >>> predict_result = model.predict(horizon=24)
    """
    def __init__(
        self,
        changepoint_prior_scale=0.05,
        seasonality_prior_scale=10.0,
        holidays_prior_scale=10.0,
        seasonality_mode='additive',
        changepoint_range=0.8,
        metric="mse",
    ):
        """
        Build a Prophet Forecast Model.
        User can customize changepoint_prior_scale, seasonality_prior_scale,
        holidays_prior_scale, seasonality_mode, changepoint_range and metric
        of the Prophet model, for details of the Prophet model hyperparameters, refer to
        https://facebook.github.io/prophet/docs/diagnostics.html#hyperparameter-tuning.

        :param changepoint_prior_scale: hyperparameter changepoint_prior_scale for the
            Prophet model.
        :param seasonality_prior_scale: hyperparameter seasonality_prior_scale for the
            Prophet model.
        :param holidays_prior_scale: hyperparameter holidays_prior_scale for the
            Prophet model.
        :param seasonality_mode: hyperparameter seasonality_mode for the
            Prophet model.
        :param changepoint_range: hyperparameter changepoint_range for the
            Prophet model.
        :param metric: the metric for validation and evaluation. For regression, we support
            Mean Squared Error: ("mean_squared_error", "MSE" or "mse"),
            Mean Absolute Error: ("mean_absolute_error","MAE" or "mae"),
            Mean Absolute Percentage Error: ("mean_absolute_percentage_error", "MAPE", "mape")
            Cosine Proximity: ("cosine_proximity", "cosine")
        """
        self.model_config = {
            "changepoint_prior_scale": changepoint_prior_scale,
            "seasonality_prior_scale": seasonality_prior_scale,
            "holidays_prior_scale": holidays_prior_scale,
            "seasonality_mode": seasonality_mode,
            "changepoint_range": changepoint_range,
            "metric": metric
        }
        self.internal = ProphetModel()

        super().__init__()

    def fit(self, data, validation_data):
        """
        Fit(Train) the forecaster.

        :param data: training data, a pandas dataframe with Td rows,
            and 2 columns, with column 'ds' indicating date and column 'y' indicating value
            and Td is the time dimension
        :param validation_data: evaluation data, should be the same type as x
        """
        self._check_data(data, validation_data)
        data_dict = {
            'x': data,
            'y': None,
            'val_x': None,
            'val_y': validation_data
        }
        return self.internal.fit_eval(data=data_dict, **self.model_config)

    def _check_data(self, data, validation_data):
        assert 'ds' in data.columns and 'y' in data.columns, \
            "data should be a pandas dataframe that has at least 2 columns 'ds' and 'y'."
        assert 'ds' in validation_data.columns and 'y' in validation_data.columns, \
            "validation_data should be a dataframe that has at least 2 columns 'ds' and 'y'."

    def predict(self, horizon):
        """
        Predict using a trained forecaster.

        :param horizon: the number of steps forward to predict
        """
        if self.internal.model is None:
            raise RuntimeError(
                "You must call fit or restore first before calling predict!")
        return self.internal.predict(horizon=horizon)

    def evaluate(self, validation_data, metrics=['mse']):
        """
        Evaluate using a trained forecaster.

        :param validation_data: evaluation data, a pandas dataframe with Td rows,
            and 2 columns, with column 'ds' indicating date and column 'y' indicating value
            and Td is the time dimension
        :param data: We don't support input data currently.
        :param metrics: A list contains metrics for test/valid data.
        """
        if validation_data is None:
            raise ValueError("Input invalid validation_data of None")
        if self.internal.model is None:
            raise RuntimeError(
                "You must call fit or restore first before calling evaluate!")
        return self.internal.evaluate(None, validation_data, metrics=metrics)

    def save(self, checkpoint_file):
        """
        Save the forecaster.

        :param checkpoint_file: The location you want to save the forecaster, should be a json file
        """
        if self.internal.model is None:
            raise RuntimeError(
                "You must call fit or restore first before calling save!")
        self.internal.save(checkpoint_file)

    def restore(self, checkpoint_file):
        """
        Restore the forecaster.

        :param checkpoint_file: The checkpoint file location you want to load the forecaster.
        """
        self.internal.restore(checkpoint_file)