Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

vm.pp.mad_scale

Robustly center or scale a numeric vector or matrix.

Signature

def mad_scale(
    x: numpy.ndarray | pandas.Series | pandas.DataFrame,
    *,
    axis: int = 0,
    center: bool = True,
    scale: bool = True,
    constant: float = 1.4826,
    na_rm: bool = True,
    zero_mad: str = "zero",
) -> numpy.ndarray | pandas.Series | pandas.DataFrame: ...

Parameters

NameTypeDefaultDescription
x`numpy.ndarraypandas.Seriespandas.DataFrame`
axisint0Scale columns with 0 or rows with 1. Ignored for vectors.
centerboolTrueSubtract the median.
scaleboolTrueDivide by the MAD multiplied by constant.
constantfloat1.4826Positive MAD scaling factor. Use 1 for raw MAD.
na_rmboolTrueIgnore NaNs when estimating location and spread.
zero_madstr'zero'Use zero (zero), return missing values (na), or raise (error) for a slice with zero MAD.

Returns

numpy.ndarray, pandas.Series, or pandas.DataFrame

Scaled data in the input container type.

Raises

TypeError

If the input is not numeric or Boolean options have invalid types.

ValueError

If the axis or scale factor is invalid, values contain infinities, or a zero MAD is encountered with zero_mad='error'.

Notes

The default scaling factor (1.4826) converts raw MAD to the normal consistency scale. Set constant=1 to use raw MAD. Missing values are preserved in the result. This function does not modify its input.

See Also

Examples

import verymadpy as vm

scaled = vm.pp.mad_scale(adata.obs["total_counts"])