Beispiel #1
0
    def transform(self, X):
        """Center and scale the data.

        Can be called on sparse input, provided that ``RobustScaler`` has been
        fitted to dense input and ``with_centering=False``.

        Parameters
        ----------
        X : {array-like, sparse matrix}
            The data used to scale along the specified axis.

        This implementation was copied and modified from Scikit-Learn.

        See License information here:
        https://github.com/scikit-learn/scikit-learn/blob/master/README.rst
        """
        if self.with_centering:
            check_is_fitted(self, "center_")
        if self.with_scaling:
            check_is_fitted(self, "scale_")
        X = self._check_array(X, self.copy)

        # if sparse.issparse(X):
        #     if self.with_scaling:
        #         inplace_column_scale(X, 1.0 / self.scale_)
        # else:
        if self.with_centering:
            X -= self.center_
        if self.with_scaling:
            X /= self.scale_
        return X
Beispiel #2
0
    def transform(self, X, y=None):
        """Transform the columns in ``X`` according to ``self.categories_``.

        Parameters
        ----------
        X : pandas.DataFrame or dask.DataFrame
        y : ignored

        Returns
        -------
        X_trn : pandas.DataFrame or dask.DataFrame
            Same type as the input. The columns in ``self.categories_`` will
            be converted to categorical dtype.
        """
        check_is_fitted(self, "categories_")
        X = self._check_array(X).copy()
        categories = self.categories_

        for k, dtype in categories.items():
            if _HAS_CTD:
                if not isinstance(dtype, pd.api.types.CategoricalDtype):
                    dtype = pd.api.types.CategoricalDtype(*dtype)
                X[k] = X[k].astype(dtype)
            else:
                cat, ordered = dtype
                X[k] = X[k].astype("category").cat.set_categories(cat, ordered)

        return X
Beispiel #3
0
    def inverse_transform(
            self, X: Union[ArrayLike,
                           DataFrameType]) -> Union[ArrayLike, DataFrameType]:
        """Scale back the data to the original representation

        Parameters
        ----------
        X : array-like
            The data used to scale along the specified axis.

        This implementation was copied and modified from Scikit-Learn.

        See License information here:
        https://github.com/scikit-learn/scikit-learn/blob/master/README.rst
        """
        check_is_fitted(self, ["center_", "scale_"])

        # if sparse.issparse(X):
        #     if self.with_scaling:
        #         inplace_column_scale(X, self.scale_)
        # else:
        if self.with_scaling:
            X *= self.scale_
        if self.with_centering:
            X += self.center_
        return X