mcpField
A
anomaly-detector

anomaly-detector

github.com/majesticlabs-dev数据与数据库5076 次安装7 周前更新
代码仓库

关于 anomaly-detector

面向数据工程师和分析师的异常检测技能,提供适用于表格数据和时间序列的离群值检测函数,涵盖统计、密度及集成方法。

主要功能

  1. 01Z-score 与 IQR 检测
  2. 02Modified Z-score 检测
  3. 03Isolation Forest 与 LOF
  4. 04Rolling 和 STL 时间序列检测
  5. 05多种方法集成投票

适用场景

  1. 01标记单列数据中的离群值
  2. 02检测多个数值列中的异常
  3. 03查找趋势或季节性序列中的异常

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/majesticlabs-dev/majestic-marketplace --skill anomaly-detector

安装后在 Claude Code 中输入/anomaly-detector确认已加载。

附带文件scripts/anomaly_detection.py

详细介绍

异常检测器

适用对象: 检测数据集异常值的数据工程师和分析师。

目标: 为各种数据类型提供可用于生产环境的异常检测函数。

脚本

从 scripts/anomaly_detection.py 执行检测函数:

from scripts.anomaly_detection import (
    detect_anomalies_zscore,
    detect_anomalies_iqr,
    detect_anomalies_modified_zscore,
    detect_anomalies_isolation_forest,
    detect_anomalies_lof,
    detect_anomalies_rolling,
    detect_anomalies_stl,
    detect_anomalies_ensemble
)

方法选择

方法最适用场景局限性
Z-Score正态分布对异常值敏感
IQR偏态分布整体灵敏度较低
Modified Z-Score稳健检测计算速度较慢
Isolation Forest高维数据需要调参
LOF局部密度异常计算开销大
Rolling存在趋势的时间序列对窗口大小敏感
STL季节性时间序列需要已知周期

使用示例

单列检测
import pandas as pd
from scripts.anomaly_detection import detect_anomalies_zscore, detect_anomalies_iqr

df = pd.read_csv('data.csv')

# Z-score method (good for normal distributions)
anomalies_z = detect_anomalies_zscore(df['value'], threshold=3.0)

# IQR method (robust to skewed data)
anomalies_iqr = detect_anomalies_iqr(df['value'], multiplier=1.5)

print(f"Z-score found {anomalies_z.sum()} anomalies")
print(f"IQR found {anomalies_iqr.sum()} anomalies")
使用 Isolation Forest 进行多列检测
from scripts.anomaly_detection import detect_anomalies_isolation_forest

numeric_cols = ['revenue', 'quantity', 'price']
anomalies = detect_anomalies_isolation_forest(df, numeric_cols, contamination=0.01)

df_anomalies = df[anomalies]
集成方法(推荐)
from scripts.anomaly_detection import detect_anomalies_ensemble

results = detect_anomalies_ensemble(
    df,
    columns=['revenue', 'quantity'],
    methods=['zscore', 'iqr', 'isolation_forest'],
    min_agreement=2  # Flag if 2+ methods agree
)

confirmed_anomalies = df[results['is_anomaly']]
时间序列异常
from scripts.anomaly_detection import detect_anomalies_rolling, detect_anomalies_stl

# Rolling window (for trending data)
anomalies = detect_anomalies_rolling(df['daily_sales'], window=7, n_std=2.0)

# STL decomposition (for seasonal data)
anomalies = detect_anomalies_stl(df['monthly_revenue'], period=12, threshold=3.0)

依赖项

pandas
numpy
scikit-learn  # For Isolation Forest, LOF
statsmodels   # For STL decomposition

评论

支持 Markdown