pyculiarity 时间序列(异常流量)异常检测初探——感觉还可以,和Facebook的fbprophet本质上一样
demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from pyculiarity import detect_ts import matplotlib.pyplot as plt import pandas as pd import matplotlib matplotlib.style.use( 'ggplot' ) __author__ = 'willmcginnis' if __name__ = = '__main__' : # first run the models twitter_example_data = pd.read_csv( './raw_data.csv' , usecols = [ 'timestamp' , 'count' ]) results = detect_ts(twitter_example_data, max_anoms = 0.05 , alpha = 0.001 , direction = 'both' , only_last = None ) # format the twitter data nicely twitter_example_data[ 'timestamp' ] = pd.to_datetime(twitter_example_data[ 'timestamp' ]) twitter_example_data.set_index( 'timestamp' , drop = True ) # make a nice plot f, ax = plt.subplots( 2 , 1 , sharex = True ) ax[ 0 ].plot(twitter_example_data[ 'timestamp' ], twitter_example_data[ 'value' ], 'b' ) ax[ 0 ].plot(results[ 'anoms' ].index, results[ 'anoms' ][ 'anoms' ], 'ro' ) ax[ 0 ].set_title( 'Detected Anomalies' ) ax[ 1 ].set_xlabel( 'Time Stamp' ) ax[ 0 ].set_ylabel( 'Count' ) ax[ 1 ].plot(results[ 'anoms' ].index, results[ 'anoms' ][ 'anoms' ], 'b' ) ax[ 1 ].set_ylabel( 'Anomaly Magnitude' ) plt.show() |
demo2代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from matplotlib import pyplot as plt from pyculiarity import detect_ts import pandas as pd import numpy as np twitter_example_data = pd.read_csv( 'raw_data.csv' , usecols = [ 'timestamp' , 'count' ]) plt.plot( range ( 0 , len (twitter_example_data)), twitter_example_data[ 'count' ], "k." , label = 'points' ) results = detect_ts(twitter_example_data, max_anoms = 0.02 , direction = 'both' ) print (results[ 'anoms' ][ 0 : 10 ]) print (results[ 'anoms' ][ - 10 :]) print ( len (results[ 'anoms' ])) for timestamp, anomal_val in zip (results[ 'anoms' ][ 'timestamp' ], results[ 'anoms' ][ 'anoms' ]): print (timestamp, anomal_val) index_list = np.where(twitter_example_data[ "timestamp" ] = = timestamp) assert len (index_list) = = 1 plt.plot([index_list[ 0 ]], anomal_val, "rX" , label = 'abnormal points' ) plt.show() |
效果图:
原始数据图:
红色为检测出来的异常点:
标签:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-10-31 查看spark是否有僵尸进程,有的话,先杀掉。可以使用下面命令