ehrapy.preprocessing.power_norm

Contents

ehrapy.preprocessing.power_norm#

ehrapy.preprocessing.power_norm(edata, vars=None, group_key=None, layer=None, copy=False, **kwargs)[source]#

Apply power transformation normalization.

Functionality is provided by PowerTransformer, see https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PowerTransformer.html for details. Note: Dask arrays are not supported for this function. Please convert to numpy array first.

Supports both 2D and 3D data:

  • 2D data: Standard normalization across observations

  • 3D data: Per-variable normalization across samples and timestamps

Parameters:
  • edata (EHRData) – Central data object. Must already be encoded using encode().

  • vars (str | Sequence[str] | None, default: None) – List of the names of the numeric variables to normalize. If None all numeric variables will be normalized.

  • group_key (str | None, default: None) – Key in edata.obs that contains group information. If provided, scaling is applied per group.

  • layer (str | None, default: None) – The layer to normalize.

  • copy (bool, default: False) – Whether to return a copy or act in place.

  • **kwargs – Additional arguments passed to the PowerTransformer.

Return type:

EHRData | None

Returns:

None if copy=False and modifies the passed edata, else returns an updated object. Also stores a record of applied normalizations as a dictionary in edata.uns[“normalization”].

Examples

>>> import ehrdata as ed
>>> import ehrapy as ep
>>> import numpy as np
>>> from scipy import stats
>>> edata = ed.dt.physionet2012(layer="tem_data")
>>> ep.pp.offset_negative_values(edata, layer="tem_data")
>>> skewed_data = np.power(edata.layers["tem_data"], 2)
>>> edata.layers["tem_data"] = skewed_data
>>> stats.skew(edata.layers["tem_data"].flatten())
504.250727
>>> ep.pp.power_norm(edata, layer="tem_data")
>>> stats.skew(edata.layers["tem_data"].flatten())
0.144324