Skip to content

min_max

MinMaxNormaliser

Bases: OneToOneInversableDerivableTransformer

Transform series by applying a Min-Max normalization.

Parameters:

Name Type Description Default
- min (float

value set to 0 after normalization (default to None and then set to the minimum value of the serie).

required
- max (float

value set to 1 after normalization (default to None and then set to the maximum value of the serie).

required
Source code in eki_mmo_equations/one_to_one_transformations/normalisation_functions/min_max.py
class MinMaxNormaliser(OneToOneInversableDerivableTransformer):
    """Transform series by applying a Min-Max normalization.

    ```math
        \\frac{serie-min}{max-min}
    ```

    Args:
        - min (float): value set to 0 after normalization
                       (default to None and then set to the minimum value of the serie).
        - max (float): value set to 1 after normalization
                       (default to None and then set to the maximum value of the serie).
    """

    def __init__(self, min=None, max=None) -> None:
        self.min = min
        self.max = max

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

    @property
    def is_linear(self) -> bool:
        return self.min == 0

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

    def fit(self, serie: np.ndarray, y=None):
        if self.max is None:
            if np.any(serie[serie > 0]):
                self.max = serie.max()
            else:
                self.max = serie.min() or 1
        if self.min is None:
            self.min = serie.min()

        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.min, self.max)

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

        return self._inverse_transformer(serie, self.min, self.max)

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

        return self._derivative_transformer(serie, self.min, self.max)

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

    @staticmethod
    def _transformer(serie: np.ndarray, min, max) -> np.ndarray:
        if ((min is None) & (max is None)) or (
            (abs(max - 1) < FLOAT_TOLERANCE_THRESHOLD) & (abs(min) < FLOAT_TOLERANCE_THRESHOLD)
        ):
            return serie
        return (serie - min) / (max - min)

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

    @staticmethod
    def _derivative_transformer(serie: np.ndarray, min, max) -> np.ndarray:
        return max - min

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

    def check_params(self, serie: np.ndarray):
        """Check if parameters respect their application scope."""
        if self.min is None:
            raise ParameterScopeException("The parameter 'min' should not be None")
        if self.max is None:
            raise ParameterScopeException("The parameter 'max' should not be None")
        if self.min == np.inf and self.min == -np.inf:
            raise ParameterScopeException(f"The parameter 'min' should be different than infinite. min: {self.min}")
        if self.max == np.inf and self.max == -np.inf:
            raise ParameterScopeException(f"The parameter 'max' should be different than infinite. max: {self.max}")
        if not self.min != self.max:
            raise ParameterScopeException(
                f"The parameters 'min' and 'max' should be different. min: {self.min}, max: {self.max}."
            )

check_params(serie)

Check if parameters respect their application scope.

Source code in eki_mmo_equations/one_to_one_transformations/normalisation_functions/min_max.py
def check_params(self, serie: np.ndarray):
    """Check if parameters respect their application scope."""
    if self.min is None:
        raise ParameterScopeException("The parameter 'min' should not be None")
    if self.max is None:
        raise ParameterScopeException("The parameter 'max' should not be None")
    if self.min == np.inf and self.min == -np.inf:
        raise ParameterScopeException(f"The parameter 'min' should be different than infinite. min: {self.min}")
    if self.max == np.inf and self.max == -np.inf:
        raise ParameterScopeException(f"The parameter 'max' should be different than infinite. max: {self.max}")
    if not self.min != self.max:
        raise ParameterScopeException(
            f"The parameters 'min' and 'max' should be different. min: {self.min}, max: {self.max}."
        )