Generate point outliers in time series¶
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.outliers import LocalZScoreGenerator, RandomZerosGenerator
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.outliers import LocalZScoreGenerator, RandomZerosGenerator
Setup random generator¶
In [2]:
Copied!
from numpy.random import default_rng
seed = 0
rng = default_rng(seed)
from numpy.random import default_rng
seed = 0
rng = default_rng(seed)
Import data (using sktime)¶
In [3]:
Copied!
from sktime.datasets import load_airline
from sktime.datasets import load_airline
In [4]:
Copied!
X = load_airline()
t = X.index.to_timestamp()
X = load_airline()
t = X.index.to_timestamp()
In [5]:
Copied!
plt.plot(t, X.values)
plt.plot(t, X.values)
Out[5]:
[<matplotlib.lines.Line2D at 0x24c31804950>]
Randomly change values to zero¶
In [6]:
Copied!
generator = RandomZerosGenerator(random_generator=rng)
generator = RandomZerosGenerator(random_generator=rng)
In [7]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, n_outliers=10)
Xt, _ = generator.generate(X.copy(), y=None, n_outliers=10)
In [8]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
Generate local extreme values (1 Dimension)¶
In [9]:
Copied!
generator = LocalZScoreGenerator(random_generator=rng)
generator = LocalZScoreGenerator(random_generator=rng)
In [10]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, n_outliers=10,)
Xt, _ = generator.generate(X.copy(), y=None, n_outliers=10,)
In [11]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(t, X.values)
axes[0].set_title('Original data')
axes[1].plot(t, Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
Generate local extreme values (2 Dimensions and more)¶
In [12]:
Copied!
X = np.random.normal(loc=(2,0), scale=(0.5, 0.1), size=(100,2))
X = np.random.normal(loc=(2,0), scale=(0.5, 0.1), size=(100,2))
In [13]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, n_outliers=25)
Xt, _ = generator.generate(X.copy(), y=None, n_outliers=25)
In [14]:
Copied!
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(X)
axes[0].set_title('Original data')
axes[1].plot(Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
fig, axes = plt.subplots(2, sharex=True, sharey=True, figsize=(6,6))
axes[0].plot(X)
axes[0].set_title('Original data')
axes[1].plot(Xt)
axes[1].set_title('Transformed data')
plt.tight_layout();
In [ ]:
Copied!