%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import adtk.detector as detector
import adtk.transformer as transformer
import adtk.aggregator as aggregator
from adtk.visualization import plot
rand = np.random.RandomState(123)
s = pd.Series(np.cumsum(rand.normal(size=300)), index=pd.date_range(start="2017-1-1", periods=300, freq="D"))
plot(s);
anomaly = detector.QuantileAD(low=0.05, high=0.95).fit_detect(s, return_list=False)
plot(s, anomaly);
anomaly = detector.QuantileAD(low=0.05, high=0.95).fit_detect(s, return_list=False)
plot(s, anomaly, anomaly_tag="marker");
anomaly = detector.QuantileAD(low=0.05, high=0.95).fit_detect(s, return_list=True)
plot(s, anomaly);
anomaly = detector.QuantileAD(low=0.05, high=0.95).fit_detect(s, return_list=True)
plot(s, anomaly, anomaly_tag="marker");
plot(None, anomaly);
low = detector.QuantileAD(low=0.05).fit_detect(s, return_list=True)
high = detector.QuantileAD(high=0.95).fit_detect(s, return_list=True)
anomaly = {"low": low, "high": high}
plot(s, anomaly);
plot(s, anomaly, anomaly_tag="marker");
low = detector.QuantileAD(low=0.05).fit_detect(s, return_list=False)
high = detector.QuantileAD(high=0.95).fit_detect(s, return_list=False)
anomaly = {"low": low, "high": high}
plot(s, anomaly);
plot(s, anomaly, anomaly_tag="marker");
plot(None, anomaly);
df = pd.concat([s.rename('A'), (s**2/10).rename("B"), (-s*2).rename("C")], axis=1)
plot(df);
plot(df, curve_group='all');
plot(df, curve_group=[["A", "B"], ["B", "C"], "C"]);
anomaly = detector.PcaAD(k=1, c=1).fit_detect(df)
plot(df, anomaly);
plot(df, anomaly, anomaly_tag="marker");
anomaly = detector.ThresholdAD(low=-10, high=15).detect(df)
plot(df, anomaly);
plot(df, anomaly, anomaly_tag="marker");
plot(df, anomaly, curve_group="all", anomaly_tag="marker");
anomaly = {
"low": detector.QuantileAD(low=0.1).fit_detect(df),
"high": detector.QuantileAD(high=0.9).fit_detect(df)
}
plot(df, anomaly);
plot(df, anomaly, anomaly_tag="marker");
plot(None, anomaly);
anomaly = {
"low": detector.QuantileAD(low=0.1).fit_detect(df),
"high": detector.QuantileAD(high=0.9).fit_detect(df),
"pca": detector.PcaAD(k=1).fit_detect(df)
}
plot(df, anomaly);
plot(df, anomaly, anomaly_tag="marker");
plot(df, anomaly, anomaly_tag={"low":"marker", "high":"marker"});
plot(df, anomaly, curve_group=[("A", "B"), ("B", "C"), ("C", "A")], anomaly_tag={"low":"marker", "high":"marker"});
plot(None, anomaly);
plot(df, anomaly, curve_group=[("A", "B"), ("B", "C"), ("C", "A")],
ts_linewidth=1,
ts_marker='v',
ts_markersize=3,
ts_alpha={"C": 0.2},
ts_color={"A":"k"},
anomaly_tag={"low":"marker", "high":"marker"},
anomaly_color={"low":{"B":"m"}},
anomaly_alpha=0.9,
anomaly_marker={"high":"+"},
anomaly_markersize={"low":{"A": 10}},
freq_as_period=False,
figsize=(16, 20)
);
f, axes = plt.subplots(nrows=3, figsize=(12,10))
plot(df, anomaly, curve_group=[("A", "B"), ("B", "C"), ("C", "A")],
ts_linewidth=1,
ts_marker='v',
ts_markersize=3,
ts_alpha={"C": 0.2},
ts_color={"A":"k"},
anomaly_tag={"low":"marker", "high":"marker"},
anomaly_color={"low":{"B":"m"}},
anomaly_alpha=0.9,
anomaly_marker={"high":"+"},
anomaly_markersize={"low":{"A": 10}},
freq_as_period=False,
axes=axes,
legend=False
);
plot();