In [1]:
%pylab inline
rcParams['figure.figsize'] = [16, 3]
import pandas as pd
In [2]:
from AnomalyDetection import detect_anomalies
In [3]:
df = pd.read_csv('data/sample1.csv', parse_dates=True, index_col=0)
data = df['Counter']
data.head(10)
Out[3]:
In [4]:
anomalies, thresholds = detect_anomalies(data, lag=20, num_anomalies=10)
In [5]:
df = pd.read_csv('data/sample2.csv', parse_dates=True, index_col=0)
data = df['Value']
In [6]:
anomalies, thresholds = detect_anomalies(data, lag=5, num_anomalies=1)
The demo here is for evaluting the effectiveness of the algorithm on historical data. To deploy the model for real-time detection on streaming data, follow this C++ API.
typedef std::vector<float> FloatVec; class AnomalyDetector { public: /// Alert Thresholds, which can be updated (if needed) live without breaking the service FloatVec Thresholds; /// Instantiate the detector from window size and Thresholds AnomalyDetector(int windowSize, FloatVec>& thresholds); /// Instantiate the detector from a saved model file AnomalyDetector(const char* modelFile); /// Save the detector to a model file void Save(const char* modelFile); /// Predict takes an incoming counter value and returns the alert level (0 means no anomaly). /// It also produces auxiliary outputs such as trend and the anomaly score int Predict(float value, float& trend, float& score); };
We also provide a bulk predict API for handling multiple signals in parallel (via OpenMP).
class BulkAnomalyDetector { 9se public: std::vector<FloatVec> Thresholds; /// Instantiate the detectors from a zipped container of model files BulkAnomalyDetector(const char* modelsContainerFile); std::vector<int> BulkPredict(const FloatVec& value, FloatVec& trends, FloatVec& scores); };