aidevelopementtoolkit.data_utils.preprocessing

standardize(data: np.ndarray, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = True, nan_handling: bool = False, eps: float = 1e-08) -> np.ndarray

Standardize data using mean and standard deviation.

Parameters:
  • data (ndarray) –

    Input data of any shape.

  • axis (Optional[Union[int, Tuple[int, ...]]], default: None ) –

    Axis or axes along which to compute mean and std. If None, statistics are computed over the entire array.

  • keepdims (bool, default: True ) –

    Whether the reduced axes are kept as dimensions of size 1.

  • nan_handling (bool, default: False ) –

    When True, uses np.nanmean and np.nanstd to ignore NaN values.

  • eps (float, default: 1e-8 ) –

    Small constant added to the standard deviation to avoid division by zero.

Returns:
  • ndarray

    Standardized data with the same shape as input.

Examples:

>>> data = np.random.rand(2, 3, 4).astype(np.float32)
>>> standardized = standardize(data, axis=(0, 1), keepdims=True)
>>> standardized = standardize(data, axis=1, keepdims=True, eps=1e-6)
Source code in aidevelopementtoolkit/data_utils/preprocessing.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def standardize(
        data: np.ndarray,
        axis: Optional[Union[int, Tuple[int, ...]]] = None,
        keepdims: bool = True,
        nan_handling: bool = False,
        eps: float = 1e-8,
    ) -> np.ndarray:
    """Standardize data using mean and standard deviation.

    Parameters
    ----------
    data : np.ndarray
        Input data of any shape.

    axis : Optional[Union[int, Tuple[int, ...]]], default=None
        Axis or axes along which to compute mean and std.
        If `None`, statistics are computed over the entire array.

    keepdims : bool, default=True
        Whether the reduced axes are kept as dimensions of size 1.

    nan_handling : bool, default=False
        When `True`, uses `np.nanmean` and `np.nanstd` to ignore NaN values.

    eps : float, default=1e-8
        Small constant added to the standard deviation to avoid division by
        zero.

    Returns
    -------
    np.ndarray
        Standardized data with the same shape as input.

    Examples
    --------
    >>> data = np.random.rand(2, 3, 4).astype(np.float32)
    >>> standardized = standardize(data, axis=(0, 1), keepdims=True)
    >>> standardized = standardize(data, axis=1, keepdims=True, eps=1e-6)
    """

    data = np.asarray(data, dtype=np.float32)

    mean_fn = np.nanmean if nan_handling else np.mean
    std_fn = np.nanstd if nan_handling else np.std

    mean = mean_fn(data, axis=axis, keepdims=keepdims)
    std = std_fn(data, axis=axis, keepdims=keepdims)
    std = np.where(std < eps, eps, std)

    return (data - mean) / std

min_max_scaling(data: np.ndarray, range: Optional[Tuple[float, float]] = None, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = True, nan_handling: bool = False) -> np.ndarray

Apply min-max scaling to data.

Parameters:
  • data (ndarray) –

    Input data of any shape.

  • range (Optional[Tuple[float, float]], default: None ) –

    Target scaling range (min, max). If None, the data is scaled to [0, 1] using statistics computed over axis.

  • axis (Optional[Union[int, Tuple[int, ...]]], default: None ) –

    Axis or axes along which to compute min and max. If None, statistics are computed over the entire array. Ignored when range is provided.

  • keepdims (bool, default: True ) –

    Whether the reduced axes are kept as dimensions of size 1. Ignored when range is provided.

  • nan_handling (bool, default: False ) –

    When True, uses np.nanmin and np.nanmax to ignore NaN values.

Returns:
  • ndarray

    Scaled data with the same shape as input.

Examples:

>>> data = np.array([[[0.0], [1.0]], [[2.0], [3.0]]], dtype=np.float32)
>>> scaled = min_max_scaling(data, range=(0.0, 1.0))
>>> scaled.min(), scaled.max()
(0.0, 1.0)
>>> scaled = min_max_scaling(data, axis=1, keepdims=True)
Source code in aidevelopementtoolkit/data_utils/preprocessing.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def min_max_scaling(
        data: np.ndarray,
        range: Optional[Tuple[float, float]] = None,
        axis: Optional[Union[int, Tuple[int, ...]]] = None,
        keepdims: bool = True,
        nan_handling: bool = False,
    ) -> np.ndarray:
    """Apply min-max scaling to data.

    Parameters
    ----------
    data : np.ndarray
        Input data of any shape.

    range : Optional[Tuple[float, float]], default=None
        Target scaling range `(min, max)`.
        If `None`, the data is scaled to `[0, 1]` using statistics computed
        over `axis`.

    axis : Optional[Union[int, Tuple[int, ...]]], default=None
        Axis or axes along which to compute min and max.
        If `None`, statistics are computed over the entire array.
        Ignored when `range` is provided.

    keepdims : bool, default=True
        Whether the reduced axes are kept as dimensions of size 1.
        Ignored when `range` is provided.

    nan_handling : bool, default=False
        When `True`, uses `np.nanmin` and `np.nanmax` to ignore NaN values.

    Returns
    -------
    np.ndarray
        Scaled data with the same shape as input.

    Examples
    --------
    >>> data = np.array([[[0.0], [1.0]], [[2.0], [3.0]]], dtype=np.float32)
    >>> scaled = min_max_scaling(data, range=(0.0, 1.0))
    >>> scaled.min(), scaled.max()
    (0.0, 1.0)
    >>> scaled = min_max_scaling(data, axis=1, keepdims=True)
    """

    data = np.asarray(data, dtype=np.float32)

    min_fn = np.nanmin if nan_handling else np.min
    max_fn = np.nanmax if nan_handling else np.max

    if range is not None:
        if len(range) != 2 or range[0] >= range[1]:
            logger.error("The given range must be a tuple `(min, max)` with min < max.")
            raise ValueError()

        target_min, target_max = range
        data_min = min_fn(data)
        data_max = max_fn(data)

    else:
        target_min, target_max = 0.0, 1.0
        data_min = min_fn(data, axis=axis, keepdims=keepdims)
        data_max = max_fn(data, axis=axis, keepdims=keepdims)

    denominator = data_max - data_min

    # Avoid division by zero for constant features
    denominator = np.where(denominator == 0, 1.0, denominator)

    scaled = (data - data_min) / denominator

    return scaled * (target_max - target_min) + target_min