Generate missing data in time series¶
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.missingness import MissingAtRandomGenerator
import numpy as np
import matplotlib.pyplot as plt
from badgers.generators.time_series.missingness import MissingAtRandomGenerator
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 0x2e7bd6568d0>]
Missing randomly completly at random (1 Dimension)¶
In [6]:
Copied!
generator = MissingAtRandomGenerator(random_generator=rng)
generator = MissingAtRandomGenerator(random_generator=rng)
In [7]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, n_missing=10)
Xt, _ = generator.generate(X.copy(), y=None, n_missing=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();
Missing randomly completly at random (2 Dimensions and more)¶
In [19]:
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 [20]:
Copied!
Xt, _ = generator.generate(X.copy(), y=None, n_missing=25)
Xt, _ = generator.generate(X.copy(), y=None, n_missing=25)
In [21]:
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!
In [ ]:
Copied!