Skip to content

noise

GlobalGaussianNoiseGenerator

Bases: NoiseGenerator

Source code in badgers/generators/time_series/noise.py
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
class GlobalGaussianNoiseGenerator(NoiseGenerator):
    def __init__(self, random_generator=default_rng(seed=0), noise_std: float = 0.1):
        """

        :param random_generator: A random generator
        :param noise_std: The standard deviation of the noise to be added
        """
        super().__init__(random_generator=random_generator)
        self.noise_std = noise_std

    def generate(self, X, y, **params):
        """
        Add Gaussian white noise to the data.
        the data is first standardized (each column has a mean = 0 and variance = 1).
        The noise is generated from a normal distribution with standard deviation = `noise_std`.
        The noise is added to the data.

        :param X:
        :return:
        """
        scaler = StandardScaler()
        # fit, transform
        scaler.fit(X)
        Xt = scaler.transform(X)
        # add noise
        Xt = Xt + self.random_generator.normal(loc=0, scale=self.noise_std, size=Xt.shape)
        # inverse standardization
        return scaler.inverse_transform(Xt), y

__init__(random_generator=default_rng(seed=0), noise_std=0.1)

:param random_generator: A random generator :param noise_std: The standard deviation of the noise to be added

Source code in badgers/generators/time_series/noise.py
63
64
65
66
67
68
69
70
def __init__(self, random_generator=default_rng(seed=0), noise_std: float = 0.1):
    """

    :param random_generator: A random generator
    :param noise_std: The standard deviation of the noise to be added
    """
    super().__init__(random_generator=random_generator)
    self.noise_std = noise_std

generate(X, y, **params)

Add Gaussian white noise to the data. the data is first standardized (each column has a mean = 0 and variance = 1). The noise is generated from a normal distribution with standard deviation = noise_std. The noise is added to the data.

:param X: :return:

Source code in badgers/generators/time_series/noise.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def generate(self, X, y, **params):
    """
    Add Gaussian white noise to the data.
    the data is first standardized (each column has a mean = 0 and variance = 1).
    The noise is generated from a normal distribution with standard deviation = `noise_std`.
    The noise is added to the data.

    :param X:
    :return:
    """
    scaler = StandardScaler()
    # fit, transform
    scaler.fit(X)
    Xt = scaler.transform(X)
    # add noise
    Xt = Xt + self.random_generator.normal(loc=0, scale=self.noise_std, size=Xt.shape)
    # inverse standardization
    return scaler.inverse_transform(Xt), y

NoiseGenerator

Bases: GeneratorMixin

Base class for transformers that add noise to tabular data

Source code in badgers/generators/time_series/noise.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class NoiseGenerator(GeneratorMixin):
    """
    Base class for transformers that add noise to tabular data
    """

    def __init__(self, random_generator=default_rng(seed=0)):
        """
        :param random_generator: A random generator
        """
        self.random_generator = random_generator

    @abc.abstractmethod
    def generate(self, X, y, **params) -> Tuple:
        pass

__init__(random_generator=default_rng(seed=0))

:param random_generator: A random generator

Source code in badgers/generators/time_series/noise.py
15
16
17
18
19
def __init__(self, random_generator=default_rng(seed=0)):
    """
    :param random_generator: A random generator
    """
    self.random_generator = random_generator