Skip to content

standard

StandardNormaliser

Bases: OneToOneInversableDerivableTransformer

Transform series by applying a normal standardization.

Parameters:

Name Type Description Default
- mean (float

value set to 0 after normalization, which the transformed datapoints are centered around (default to None and then set to the mean of the serie).

required
- std (float

standard deviation of the current variable (default to None and then set to the standard deviation of the serie) used to set to 1 the standard deviation of the transformed variable.

required
Source code in eki_mmo_equations/one_to_one_transformations/normalisation_functions/standard.py
class StandardNormaliser(OneToOneInversableDerivableTransformer):
    """Transform series by applying a normal standardization.

    ```math
        \\frac{serie-mean}{std}
    ```

    Args:
        - mean (float): value set to 0 after normalization, which the transformed datapoints are centered around
                        (default to None and then set to the mean of the serie).
        - std (float): standard deviation of the current variable (default to None and then set to the standard
                       deviation of the serie) used to set to 1 the standard deviation of the transformed variable.
    """

    def __init__(self, mean=None, std=None) -> None:
        self.mean = mean
        self.std = std

    @property
    def parameters(self) -> Dict[str, float]:
        return self.__dict__

    @property
    def is_linear(self) -> bool:
        if self.mean == 0:
            is_linear = True
        else:
            is_linear = False
        return is_linear

    # ------- METHODS -------

    def fit(self, serie: np.ndarray, y=None):
        if self.std is None:
            self.std = serie.std()
        if self.mean is None:
            self.mean = serie.mean()

        return super().fit(serie, y)

    def transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        serie = super().transform(serie, copy)

        return self._transformer(serie, self.mean, self.std)

    def inverse_transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        if copy:
            serie = serie.copy()

        return self._inverse_transformer(serie, self.mean, self.std)

    def derivative_transform(self, serie: np.ndarray, copy=False) -> np.ndarray:
        serie = super().derivative_transform(serie, copy)

        return self._derivative_transformer(serie, self.std, self.mean)

    # ------- TRANSFORMERS -------

    @staticmethod
    def _transformer(serie: np.ndarray, mean, std) -> np.ndarray:
        return (serie - mean) / std

    @staticmethod
    def _inverse_transformer(serie: np.ndarray, mean, std) -> np.ndarray:
        return serie * std + mean

    @staticmethod
    def _derivative_transformer(serie: np.ndarray, std, mean) -> np.ndarray:
        return 1 / std

    # ------- CHECKERS -------

    def check_params(self, serie: np.ndarray):
        """Check if parameters respect their application scope."""
        if self.mean is None:
            raise ParameterScopeException("The parameter 'mean' should not be None")
        if self.std is None:
            raise ParameterScopeException("The parameter 'std' should not be None")
        if not self.std > 0:
            raise ParameterScopeException(f"The parameter 'std' should not be inferior to 0, not {self.std}")

check_params(serie)

Check if parameters respect their application scope.

Source code in eki_mmo_equations/one_to_one_transformations/normalisation_functions/standard.py
def check_params(self, serie: np.ndarray):
    """Check if parameters respect their application scope."""
    if self.mean is None:
        raise ParameterScopeException("The parameter 'mean' should not be None")
    if self.std is None:
        raise ParameterScopeException("The parameter 'std' should not be None")
    if not self.std > 0:
        raise ParameterScopeException(f"The parameter 'std' should not be inferior to 0, not {self.std}")