GroupNormalizer#
- class pytorch_forecasting.data.encoders.GroupNormalizer(method: str = 'standard', groups: List[str] | None = None, center: bool = True, scale_by_group: bool = False, transformation: str | Tuple[Callable, Callable] | None = None, method_kwargs: Dict[str, Any] | None = None)[source]#
Bases:
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”.
method_kwargs (Dict[str, Any], optional) – Dictionary of method specific arguments as listed below * “robust” method: “upper”, “lower”, “center” quantiles defaulting to 0.75, 0.25 and 0.5
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.transformation (Union[str, Tuple[Callable, Callable]] optional) –
Transform values before applying normalizer. Available options are
None (default): No transformation of values
log: Estimate in log-space leading to a multiplicative model
- log1p: Estimate in log-space but add 1 to values before transforming for stability
(e.g. if many small values <<1 are present). Note, that inverse transform is still only torch.exp() and not torch.expm1().
logit: Apply logit transformation on values that are between 0 and 1
- count: Apply softplus to output (inverse transformation) and x + 1 to input
(transformation)
- softplus: Apply softplus to output (inverse transformation) and inverse softplus to input
(transformation)
relu: Apply max(0, x) to output
Dict[str, Callable] of PyTorch functions that transforms and inversely transforms values.
forward
andreverse
entries are required.inverse
transformation is optional and should be defined ifreverse
is not the inverse of the forward transformation.inverse_torch
can be defined to provide a torch distribution transform for inverse transformations.
- Inherited-members:
Methods
extra_repr
()fit
(y, X)Determine scales for each group
fit_transform
(y, X[, return_norm])Fit normalizer and scale input data.
get_metadata_routing
()Get metadata routing of this object.
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.
get_transform
(transformation)Return transformation functions.
inverse_preprocess
(y)Inverse preprocess re-scaled data (e.g. take exp).
inverse_transform
(y, X)Rescaling data to original scale - not implemented - call class with target scale instead.
preprocess
(y)Preprocess input data (e.g. take log).
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
set_transform_request
(*[, return_norm, ...])Request metadata passed to the
transform
method.transform
(y[, X, return_norm, target_scale])Scale input data.
Attributes
TRANSFORMATIONS
Names of determined scales.
- fit(y: Series, X: 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: Series, X: DataFrame, return_norm: bool = False) ndarray | Tuple[ndarray, ndarray] [source]#
Fit normalizer and scale input data.
- Parameters:
y (pd.Series) – data to scale
X (pd.DataFrame) – dataframe with
groups
columnsreturn_norm (bool, optional) – If to return . Defaults to False.
- Returns:
- Scaled data, if
return_norm=True
, returns also scales as second element
- Scaled data, if
- Return type:
Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]
- get_norm(X: DataFrame) 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: Tensor | list | tuple, group_names: List[str] = None) 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 attributegroups
.
- Returns:
parameters used for scaling
- Return type:
np.ndarray
- inverse_transform(y: Series, X: DataFrame)[source]#
Rescaling data to original scale - not implemented - call class with target scale instead.
- set_transform_request(*, return_norm: bool | None | str = '$UNCHANGED$', target_scale: bool | None | str = '$UNCHANGED$') GroupNormalizer #
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it totransform
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
return_norm (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
return_norm
parameter intransform
.target_scale (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
target_scale
parameter intransform
.
- Returns:
self – The updated object.
- Return type:
object
- transform(y: Series, X: DataFrame = None, return_norm: bool = False, target_scale: Tensor = None) ndarray | Tuple[ndarray, ndarray] [source]#
Scale input data.
- Parameters:
y (pd.Series) – data to scale
X (pd.DataFrame) – dataframe with
groups
columnsreturn_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
- Scaled data, if
- Return type:
Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]
- property names: List[str]#
Names of determined scales.
- Returns:
list of names
- Return type:
List[str]