Simple Anomaly Detection

You can create and evaluate an anomaly detection model with just a few lines of code.

Provide your timeseries as a pandas dataframe with timestamp and value. Optionally, you can also provide the anomaly labels as a column in the dataframe.

For example, to detect anomalies in daily sessions data, your dataframe could look like this:

import pandas as pd
df = pd.DataFrame({
    "date": ["2020-01-08-00", "2020-01-09-00", "2020-01-10-00"],
    "sessions": [10231.0, 12309.0, 12104.0],
    "is_anomaly": [False, True, False]
})

The time column can be any format recognized by pandas.to_datetime.

In this example, we’ll load a dataset representing log(daily page views) on the Wikipedia page for Peyton Manning. It contains values from 2007-12-10 to 2016-01-20. More dataset info here.

29 import warnings
30
31 import plotly
32 from greykite.common.data_loader import DataLoader
33 from greykite.detection.detector.config import ADConfig
34 from greykite.detection.detector.data import DetectorData
35 from greykite.detection.detector.greykite import GreykiteDetector
36 from greykite.framework.templates.autogen.forecast_config import ForecastConfig
37 from greykite.framework.templates.autogen.forecast_config import MetadataParam
38 from greykite.framework.templates.model_templates import ModelTemplateEnum
39
40 warnings.filterwarnings("ignore")
41
42 # Loads dataset into pandas DataFrame
43 dl = DataLoader()
44 df = dl.load_peyton_manning()
45
46 # specify dataset information
47 metadata = MetadataParam(
48     time_col="ts",  # name of the time column ("date" in example above)
49     value_col="y",  # name of the value column ("sessions" in example above)
50     freq="D"  # "H" for hourly, "D" for daily, "W" for weekly, etc.
51     # Any format accepted by `pandas.date_range`
52 )

Create an Anomaly Detection Model

Similar to forecasting, you need to provide a forecast config and an anomaly detection config. You can choose any of the available forecast model templates (see Choose a Model).

61 # In this example, we choose the "AUTO" model template for the forecast config,
62 # and the default anomaly detection config.
63 # The Silverkite "AUTO" model template chooses the parameter configuration
64 # given the input data frequency, forecast horizon and evaluation configs.
65
66 anomaly_detector = GreykiteDetector()  # Creates an instance of the Greykite anomaly detector
67
68 forecast_config = ForecastConfig(
69     model_template=ModelTemplateEnum.AUTO.name,
70     forecast_horizon=7,  # forecasts 7 steps ahead
71     coverage=None,       # Confidence Interval will be tuned by the AD model
72     metadata_param=metadata)
73
74 ad_config = ADConfig()  # Default anomaly detection config
75
76 detector = GreykiteDetector(
77     forecast_config=forecast_config,
78     ad_config=ad_config,
79     reward=None)

Train the Anomaly Detection Model

You can train the anomaly detection model by calling the fit method. This method takes a DetectorData object as input. The DetectorData object consists the time series information as a pandas dataframe. Optionally, you can also provide the anomaly labels as a column in the dataframe. The anomaly labels can also be provided as a list of boolean values. The anomaly labels are used to evaluate the model performance.

91 train_size = int(2700)
92 df_train = df[:train_size].reset_index(drop=True)
93 train_data = DetectorData(df=df_train)
94 detector.fit(data=train_data)

Out:

Fitting 3 folds for each of 1 candidates, totalling 3 fits

Predict with the Anomaly Detection Model

You can predict anomalies by calling the predict method.

101 test_data = DetectorData(df=df)
102 test_data = detector.predict(test_data)

Evaluate the Anomaly Detection Model

The output of the anomaly detection model are stored as attributes of the GreykiteDetector object. (The interactive plots are generated by plotly: click to zoom!)

Training

The fitted_df attribute contains the result on the training data. You can plot the result by calling the plot method with phase="train".

117 print(detector.fitted_df)
118
119 fig = detector.plot(
120     phase="train",
121     title="Greykite Detector Peyton Manning - fit phase")
122 plotly.io.show(fig)

Out:

             ts    actual  forecast  forecast_lower  forecast_upper  is_anomaly_predicted   z_score is_anomaly
0    2007-12-10  9.590761  9.194596        9.166271        9.222921                  True  0.877043       None
1    2007-12-11  8.519590  9.022015        8.993690        9.050340                  True -1.112284       None
2    2007-12-12  8.183677  8.945369        8.917044        8.973694                  True -1.686259       None
3    2007-12-13  8.072467  8.895533        8.867208        8.923858                  True -1.822130       None
4    2007-12-14  7.893572  8.836495        8.808170        8.864820                  True -2.087475       None
...         ...       ...       ...             ...             ...                   ...       ...        ...
2753 2015-06-24  7.344073  6.915129        6.886804        6.943454                  True  0.949609       None
2754 2015-06-25  7.291656  6.937793        6.909468        6.966118                  True  0.783395       None
2755 2015-06-26  7.271704  6.872038        6.843713        6.900363                  True  0.884793       None
2756 2015-06-27  7.454720  6.485441        6.457116        6.513766                  True  2.145821       None
2757 2015-06-28  6.692084  6.709412        6.681087        6.737737                 False -0.038362       None

[2758 rows x 8 columns]

Prediction

The pred_df attribute contains the predicted result. You can plot the result by calling the plot method with phase="predict".

130 print(detector.pred_df)
131
132 fig = detector.plot(
133     phase="predict",
134     title="Greykite Detector Peyton Manning - predict phase")
135 plotly.io.show(fig)