解析异常模式、检测趋势 — ADTK Python 库

今天我要向大家介绍一个非常强大的 Python 库,它就是 ADTK(异常检测工具包)。

ADTK 提供了多种技术和算法,用于在时间序列数据中进行异常检测。无论是基于阈值的简单方法还是更复杂的统计方法,ADTK 都为处理不同类型的异常提供了灵活性。

初体验

库的安装

你可以通过 pip 直接安装 ADTK:

<code>pip install adtk</code>

加载和准备数据

在这个例子中,我们将使用苹果股票价格数据集:

<code>import pandas as pd import matplotlib.pyplot as plt import yfinance as yf from adtk.data import validate_series from adtk.visualization import plot from adtk.detector import ThresholdAD, QuantileAD, InterQuartileRangeAD, GeneralizedESDTestAD, PersistAD, VolatilityShiftAD, CustomizedDetectorHD s_train = yf.download("AAPL")['Close'] s_train = validate_series(s_train) print(s_train)</code>

确保数据是有效的时间序列,并准备用于分析。

时间序列数据可视化

数据可视化是了解时间序列数据趋势和模式的重要步骤。通过可视化,我们可以看到数据随时间的变化。

<code># 可视化时间序列数据 plot(s_train) plt.show()</code>
图片[1]-解析异常模式、检测趋势 — ADTK Python 库-山海云端论坛

这个图表显示了我们将用于异常检测的时间序列数据的模式。

异常检测技术

在 ADTK 中,有多种异常检测技术可供选择。每种技术都有不同的方法来识别异常。

基于阈值的异常检测

<code>threshold_ad = ThresholdAD(high=0.75, low=-0.5) anomaly = threshold_ad.detect(s_train) plot(s_train, anomaly=anomaly, anomaly_color="red", anomaly_tag="marker") plt.show()</code>
图片[2]-解析异常模式、检测趋势 — ADTK Python 库-山海云端论坛

基于分位数的异常检测

<code>quantile_ad = QuantileAD(high=0.99, low=0.01) anomaly = quantile_ad.fit_detect(s_train) plot(s_train, anomaly=anomaly, ts_linewidth=1, ts_markersize=3, anomaly_markersize=5, anomaly_color='red', anomaly_tag="marker") plt.show()</code>
图片[3]-解析异常模式、检测趋势 — ADTK Python 库-山海云端论坛

基于四分位距 (IQR) 的异常检测

<code>iqr_ad = InterQuartileRangeAD(c=1.5) anomaly = iqr_ad.fit_detect(s_train) plot(s_train, anomaly=anomaly, ts_linewidth=1, ts_markersize=3, anomaly_markersize=5, anomaly_color='red', anomaly_tag="marker") plt.show()</code>

基于广义ESD异常检测

<code>esd_ad = GeneralizedESDTestAD(alpha=0.3) anomaly = esd_ad.fit_detect(s_train) plot(s_train, anomaly=anomaly, ts_linewidth=1, ts_markersize=3, anomaly_markersize=5, anomaly_color='red', anomaly_tag="marker") plt.show()</code>

波动率变化异常检测

<code>volatility_shift_ad = VolatilityShiftAD(c=6.0, side='positive', window=30) anomaly = volatility_shift_ad.fit_detect(s_train) plot(s_train, anomaly=anomaly, color_anomaly='red') plt.show()</code>

通过应用这些不同的异常检测技术,我们可以了解不同方法如何检测时间序列数据中的异常。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容