Search code examples
pythonpython-2.7time-seriesanomaly-detection

Working Example Of Luminol Anomaly Detection And Correlation Library By Linkedin


Github Link Of Luminol Library: https://github.com/linkedin/luminol

Can anyone explain me with a sample code, how to use this module for finding anomalies in data set.

I want to use this module for finding the anomalies in my time series data.

P.S.: I tried the example 1 provided in README.md but getting error, so someone please provide me a working example for finding anomalies.

Example 1 Put anomaly scores in a list.

from luminol.anomaly_detector import AnomalyDetector
my_detector = AnomalyDetector(ts)
score = my_detector.get_all_scores()
anom_score = list()
for (timestamp, value) in score.iteritems():
    t_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
    anom_score.append([t_str, value])

Getting value error: (22, 'Invalid argument') In line: t_str = time.strftime('%Y-%m-%d %H :%M%S', time.localtime(timestamp))

Using Python 2.7

Thanks :)


Solution

  • The example works after adding import time and defining ts. The use of time.localtime presumes your starting data uses unix time. Additional parameters for AnomalyDetector are noted here. The available algorithms are defined here. If algorithm_name is not specified, AnomalyDetector falls back to using the the default_detector which uses a weighted sum of exponential averages and derivatives. These slides might also be helpful.

    data.csv

    1490323038, 3
    1490323048, 4
    1490323058, 6
    1490323068, 78
    1490323078, 67
    1490323088, 5
    

    app.py

    from luminol.anomaly_detector import AnomalyDetector
    import time
    
    # ts = 'data.csv'  # or
    ts = { 
        '1490323038': 3,
        '1490323048': 4,
        '1490323058': 6,
        '1490323068': 78,
        '1490323078': 67,
        '1490323088': 5,
    }
    
    my_detector = AnomalyDetector(ts)
    score = my_detector.get_all_scores()
    anom_score = []
    
    for (timestamp, value) in score.iteritems():
        t_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
        anom_score.append([t_str, value])
    
    for score in anom_score:
        print(score)
    

    Output:

    ['2017-03-23 19:37:18', 0.0]
    ['2017-03-23 19:37:28', 0.02482518793211144]
    ['2017-03-23 19:37:38', 0.06951052620991202]
    ['2017-03-23 19:37:48', 2.5187085350547482]
    ['2017-03-23 19:37:58', 1.201340494410737]
    ['2017-03-23 19:38:08', 0.9673414624904575]