Deteksi Anomali②

Anomaly Detection Toolkit (ADTK) digunakan untuk melakukan deteksi anomali. Deteksi anomali diterapkan pada data buatan multidimensi. Kali ini, deteksi anomali akan diterapkan pada data dengan beberapa dimensi.

import numpy as np
import pandas as pd
from adtk.data import validate_series

s_train = pd.read_csv("./training.csv", index_col="timestamp", parse_dates=True)
s_train = validate_series(s_train)
s_train["value2"] = s_train["value"].apply(lambda v: np.sin(v) + np.cos(v))
s_train

valuevalue2
timestamp
2014-04-01 00:00:0018.0904860.037230
2014-04-01 00:05:0020.3598431.058643
2014-04-01 00:10:0021.1054700.141581
2014-04-01 00:15:0021.1515850.076564
2014-04-01 00:20:0018.1371410.103122
.........
2014-04-14 23:35:0018.2692900.288071
2014-04-14 23:40:0019.0873511.207420
2014-04-14 23:45:0019.5946891.413067
2014-04-14 23:50:0019.7678171.401750
2014-04-14 23:55:0020.4791560.939501

4032 rows × 2 columns

from adtk.visualization import plot

plot(s_train)

png

Perbandingan Metode Deteksi Anomali

Deteksi anomali dilakukan menggunakan SeasonalAD. Metode lainnya dapat dilihat di Detector.

import matplotlib.pyplot as plt
from adtk.detector import OutlierDetector, PcaAD, RegressionAD
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import LocalOutlierFactor

model_dict = {
    "OutlierDetector": OutlierDetector(LocalOutlierFactor(contamination=0.05)),
    "RegressionAD": RegressionAD(regressor=LinearRegression(), target="value2", c=3.0),
    "PcaAD": PcaAD(k=2),
}

for model_name, model in model_dict.items():
    anomalies = model.fit_detect(s_train)

    plot(
        s_train,
        anomaly=anomalies,
        ts_linewidth=1,
        ts_markersize=3,
        anomaly_color="red",
        anomaly_alpha=0.3,
        curve_group="all",
    )
    plt.title(model_name)
    plt.show()

png

png

png

Komentar

(Komentar akan muncul setelah disetujui)