GroupNormalizer

class pytorch_forecasting.data.encoders.GroupNormalizer(method: str = 'standard', groups: List[str] = [], center: bool = True, scale_by_group: bool = False, log_scale: Union[bool, float] = False, log_zero_value: float = 0.0, coerce_positive: Union[float, bool] = None, eps: float = 1e-08)[source]

Bases: pytorch_forecasting.data.encoders.TorchNormalizer

Normalizer that scales by groups.

For each group a scaler is fitted and applied. This scaler can be used as target normalizer or also to normalize any other variable.

Group normalizer to normalize a given entry by groups. Can be used as target normalizer.

Parameters
  • method (str, optional) – method to rescale series. Either “standard” (standard scaling) or “robust” (scale using quantiles 0.25-0.75). Defaults to “standard”.

  • groups (List[str], optional) – Group names to normalize by. Defaults to [].

  • center (bool, optional) – If to center the output to zero. Defaults to True.

  • scale_by_group (bool, optional) – If to scale the output by group, i.e. norm is calculated as (group1_norm * group2_norm * ...) ^ (1 / n_groups). Defaults to False.

  • log_scale (bool, optional) – If to take log of values. Defaults to False. Defaults to False.

  • log_zero_value (float, optional) – Value to map 0 to for log_scale=True or in softplus. Defaults to 0.0

  • coerce_positive (Union[bool, float, str], optional) – If to coerce output to positive. Valid values: * None, i.e. is automatically determined and might change to True if all values are >= 0 (Default). * True, i.e. output is clamped at 0. * False, i.e. values are not coerced * float, i.e. softmax is applied with beta = coerce_positive.

  • eps (float, optional) – Number for numerical stability of calcualtions. Defaults to 1e-8. For count data, 1.0 is recommended.

Inherited-members

Methods

fit(y, X)

Determine scales for each group

fit_transform(y, X[, return_norm])

Fit normalizer and scale input data.

get_norm(X)

Get scaling parameters for multiple groups.

get_parameters(groups[, group_names])

Get fitted scaling parameters for a given group.

get_params([deep])

Get parameters for this estimator.

inverse_transform(y, X)

Rescaling data to original scale - not implemented - call class with target scale instead.

set_params(**params)

Set the parameters of this estimator.

transform(y[, X, return_norm, target_scale])

Scale input data.

Attributes

names

Names of determined scales.

fit(y: pandas.core.series.Series, X: pandas.core.frame.DataFrame)[source]

Determine scales for each group

Parameters
  • y (pd.Series) – input data

  • X (pd.DataFrame) – dataframe with columns for each group defined in groups parameter.

Returns

self

fit_transform(y: pandas.core.series.Series, X: pandas.core.frame.DataFrame, return_norm: bool = False) → Union[numpy.ndarray, Tuple[numpy.ndarray, numpy.ndarray]][source]

Fit normalizer and scale input data.

Parameters
  • y (pd.Series) – data to scale

  • X (pd.DataFrame) – dataframe with groups columns

  • return_norm (bool, optional) – If to return . Defaults to False.

Returns

Scaled data, if return_norm=True, returns also scales

as second element

Return type

Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]

get_norm(X: pandas.core.frame.DataFrame) → pandas.core.frame.DataFrame[source]

Get scaling parameters for multiple groups.

Parameters

X (pd.DataFrame) – dataframe with groups columns

Returns

dataframe with scaling parameterswhere each row corresponds to the input dataframe

Return type

pd.DataFrame

get_parameters(groups: Union[torch.Tensor, list, tuple], group_names: List[str] = None) → numpy.ndarray[source]

Get fitted scaling parameters for a given group.

Parameters
  • groups (Union[torch.Tensor, list, tuple]) – group ids for which to get parameters

  • group_names (List[str], optional) – Names of groups corresponding to positions in groups. Defaults to None, i.e. the instance attribute groups.

Returns

parameters used for scaling

Return type

np.ndarray

inverse_transform(y: pandas.core.series.Series, X: pandas.core.frame.DataFrame)[source]

Rescaling data to original scale - not implemented - call class with target scale instead.

transform(y: pandas.core.series.Series, X: pandas.core.frame.DataFrame = None, return_norm: bool = False, target_scale: torch.Tensor = None) → Union[numpy.ndarray, Tuple[numpy.ndarray, numpy.ndarray]][source]

Scale input data.

Parameters
  • y (pd.Series) – data to scale

  • X (pd.DataFrame) – dataframe with groups columns

  • return_norm (bool, optional) – If to return . Defaults to False.

  • target_scale (torch.Tensor) – target scale to use instead of fitted center and scale

Returns

Scaled data, if return_norm=True, returns also scales

as second element

Return type

Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]

property names

Names of determined scales.

Returns

list of names

Return type

List[str]