예제 #1
0
def _make_fh(cutoff, steps, fh_type, is_relative):
    """Helper function to construct forecasting horizons for testing"""
    from sktime.forecasting.tests._config import INDEX_TYPE_LOOKUP

    fh_class = INDEX_TYPE_LOOKUP[fh_type]

    if isinstance(steps, (int, np.integer)):
        steps = np.array([steps], dtype=np.int)

    if is_relative:
        return ForecastingHorizon(fh_class(steps), is_relative=is_relative)

    else:
        kwargs = {}

        if fh_type == "datetime":
            steps *= cutoff.freq

        if fh_type == "period":
            kwargs = {"freq": cutoff.freq}

        values = cutoff + steps
        return ForecastingHorizon(fh_class(values, **kwargs), is_relative)
예제 #2
0
    def _transform(self, X, y=None):
        """Transform X and return a transformed version.

        private _transform containing the core logic, called from transform

        Parameters
        ----------
        X : pd.Series or pd.DataFrame
            Data to be transformed
        y : pd.DataFrame, default=None
            Additional data, e.g., labels for transformation

        Returns
        -------
        Xt : pd.Series or pd.DataFrame, same type as X
            transformed version of X, detrended series
        """
        fh = ForecastingHorizon(X.index, is_relative=False)

        # univariate: X is pd.Series
        if isinstance(X, pd.Series):
            # note: the y in the transformer is exogeneous in the forecaster, i.e., X
            X_pred = self.forecaster_.predict(fh=fh, X=y)
            Xt = X - X_pred
            return Xt
        # multivariate: X is pd.DataFrame
        elif isinstance(X, pd.DataFrame):
            Xt = X.copy()
            # check if all columns are known
            X_fit_keys = set(self.forecaster_.keys())
            X_new_keys = set(X.columns)
            difference = X_new_keys.difference(X_fit_keys)
            if len(difference) != 0:
                raise ValueError("X contains columns that have not been "
                                 "seen in fit: " + str(difference))
            for colname in Xt.columns:
                X_pred = self.forecaster_[colname].predict(fh=fh, X=y)
                Xt[colname] = Xt[colname] - X_pred
            return Xt
        else:
            raise TypeError("X must be pd.Series or pd.DataFrame")
예제 #3
0
    def transform(self, Z, X=None):
        """
        Remove trend from the data.

        Parameters
        ----------
        y : pd.Series
            Time series to be detrended
        X : pd.DataFrame, optional (default=False)
            Exogenous variables

        Returns
        -------
        y_hat : pd.Series
            De-trended series
        """
        self.check_is_fitted()
        z = check_series(Z, enforce_univariate=True)
        fh = ForecastingHorizon(z.index, is_relative=False)
        z_pred = self.forecaster_.predict(fh, X)
        return z - z_pred
예제 #4
0
    def inverse_transform(self, Z, X=None):
        """
        Add trend back to a time series

        Parameters
        ----------
        y : pd.Series, list
            Detrended time series to revert
        X : pd.DataFrame, optional (default=False)
            Exogenous variables

        Returns
        -------
        y_hat : pd.Series
            Series with the trend
        """
        self.check_is_fitted()
        z = check_series(Z, enforce_univariate=True)
        fh = ForecastingHorizon(z.index, is_relative=False)
        z_pred = self.forecaster_.predict(fh, X)
        return z + z_pred
예제 #5
0
    def inverse_transform(self, y, X=None):
        """
        Add trend back to a time series

        Parameters
        ----------
        y : pd.Series, list
            Detrended time series to revert
        X : pd.DataFrame, optional (default=False)
            Exogenous variables

        Returns
        -------
        y_hat : pd.Series
            Series with the trend
        """
        self.check_is_fitted()
        y = check_y(y)
        fh = ForecastingHorizon(y.index, is_relative=False)
        y_pred = self.forecaster_.predict(fh=fh, X=X)
        return y + y_pred
예제 #6
0
    def _transform(self, X, y=None):
        """Transform X and return a transformed version.

        private _transform containing the core logic, called from transform

        Parameters
        ----------
        X : pd.Series or pd.DataFrame
            Data to be transformed
        y : pd.DataFrame, default=None
            Additional data, e.g., labels for transformation

        Returns
        -------
        Xt : pd.Series or pd.DataFrame, same type as X
            transformed version of X, detrended series
        """
        z = X
        fh = ForecastingHorizon(z.index, is_relative=False)

        # multivariate
        if isinstance(z, pd.DataFrame):
            z = z.copy()
            # check if all columns are known
            Z_fit_keys = set(self.forecaster_.keys())
            Z_new_keys = set(z.columns)
            difference = Z_new_keys.difference(Z_fit_keys)
            if len(difference) != 0:
                raise ValueError("Z contains columns that have not been "
                                 "seen in fit: " + str(difference))
            for colname in z.columns:
                z_pred = self.forecaster_[colname].predict(fh, y)
                z[colname] = z[colname] - z_pred
            Xt = z
        # univariate
        else:
            z_pred = self.forecaster_.predict(fh, y)
            Xt = z - z_pred

        return Xt
예제 #7
0
    def inverse_transform(self, Z, X=None):
        """Add trend back to a time series.

        Parameters
        ----------
        y : pd.Series, list
            Detrended time series to revert.
        X : pd.DataFrame, optional (default=False)
            Exogenous variables.

        Returns
        -------
        y_hat : pd.Series
            Series with the trend.
        """
        self.check_is_fitted()
        z = check_series(Z)
        fh = ForecastingHorizon(z.index, is_relative=False)

        # multivariate
        if isinstance(z, pd.DataFrame):
            z = z.copy()
            # check if all columns are known
            Z_fit_keys = set(self.forecaster_.keys())
            Z_new_keys = set(z.columns)
            difference = Z_new_keys.difference(Z_fit_keys)
            if len(difference) != 0:
                raise ValueError(
                    "Z contains columns that have not been "
                    "seen in fit: " + difference
                )
            for colname in z.columns:
                z_pred = self.forecaster_[colname].predict(fh, X)
                z[colname] = z[colname] + z_pred
            return z
        # univariate
        else:
            z_pred = self.forecaster_.predict(fh, X)
            return z + z_pred
예제 #8
0
    def transform(self, Z, X=None):
        """Remove trend from the data.

        Parameters
        ----------
        y : pd.Series
            Time series to be detrended.
        X : pd.DataFrame, optional (default=False)
            Exogenous variables.

        Returns
        -------
        y_hat : pd.Series
            De-trended series.
        """
        self.check_is_fitted()
        z = check_series(Z)
        fh = ForecastingHorizon(z.index, is_relative=False)

        # multivariate
        if isinstance(z, pd.DataFrame):
            z = z.copy()
            # check if all columns are known
            Z_fit_keys = set(self.forecaster_.keys())
            Z_new_keys = set(z.columns)
            difference = Z_new_keys.difference(Z_fit_keys)
            if len(difference) != 0:
                raise ValueError(
                    "Z contains columns that have not been "
                    "seen in fit: " + str(difference)
                )
            for colname in z.columns:
                z_pred = self.forecaster_[colname].predict(fh, X)
                z[colname] = z[colname] - z_pred
            return z
        # univariate
        else:
            z_pred = self.forecaster_.predict(fh, X)
            return z - z_pred
예제 #9
0
    def transform(self, y, X=None):
        """
        Remove trend from the data.

        Parameters
        ----------
        y : pd.Series, list
            Time series to be detrended
        X : pd.DataFrame, optional (default=False)
            Exogenous variables

        Returns
        -------
        y_hat : pd.Series
            De-trended series
        """
        self.check_is_fitted()
        y = check_y(y)

        fh = ForecastingHorizon(y.index, is_relative=False)
        y_pred = self.forecaster_.predict(fh, X=X)
        return y - y_pred
예제 #10
0
    def _inverse_transform(self, X, y=None):
        """Logic used by `inverse_transform` to reverse transformation on `X`.

        Parameters
        ----------
        X : pd.Series or pd.DataFrame
            Data to be inverse transformed
        y : pd.DataFrame, default=None
            Additional data, e.g., labels for transformation

        Returns
        -------
        Xt : pd.Series or pd.DataFrame, same type as X
            inverse transformed version of X
        """
        fh = ForecastingHorizon(X.index, is_relative=False)

        # univariate: X is pd.Series
        if isinstance(X, pd.Series):
            # note: the y in the transformer is exogeneous in the forecaster, i.e., X
            X_pred = self.forecaster_.predict(fh=fh, X=y)
            return X + X_pred
        # multivariate: X is pd.DataFrame
        if isinstance(X, pd.DataFrame):
            X = X.copy()
            # check if all columns are known
            X_fit_keys = set(self.forecaster_.keys())
            X_new_keys = set(X.columns)
            difference = X_new_keys.difference(X_fit_keys)
            if len(difference) != 0:
                raise ValueError("X contains columns that have not been "
                                 "seen in fit: " + str(difference))
            for colname in X.columns:
                X_pred = self.forecaster_[colname].predict(fh=fh, X=y)
                X[colname] = X[colname] + X_pred
            return X
예제 #11
0
    def _inverse_transform(self, X, y=None):
        """Logic used by `inverse_transform` to reverse transformation on `X`.

        Parameters
        ----------
        X : pd.Series or pd.DataFrame
            Data to be inverse transformed
        y : pd.DataFrame, default=None
            Additional data, e.g., labels for transformation

        Returns
        -------
        Xt : pd.Series or pd.DataFrame, same type as X
            inverse transformed version of X
        """
        z = X
        fh = ForecastingHorizon(z.index, is_relative=False)

        # multivariate
        if isinstance(z, pd.DataFrame):
            z = z.copy()
            # check if all columns are known
            Z_fit_keys = set(self.forecaster_.keys())
            Z_new_keys = set(z.columns)
            difference = Z_new_keys.difference(Z_fit_keys)
            if len(difference) != 0:
                raise ValueError("Z contains columns that have not been "
                                 "seen in fit: " + difference)
            for colname in z.columns:
                z_pred = self.forecaster_[colname].predict(fh, X)
                z[colname] = z[colname] + z_pred
            return z
        # univariate
        else:
            z_pred = self.forecaster_.predict(fh, X)
            return z + z_pred