分析热点话题的工具
项目描述
Moda
热点话题检测和异常检测的模型和评估框架
Moda提供了一个接口,用于评估单变量或多类别时间序列数据集上的模型。它还允许用户通过使用scikit-learn风格的API来添加额外的模型。Moda中提供的所有模型都已调整为多类别场景,通过包装单变量模型以在多个类别上运行。它还允许使用训练/测试拆分或时间序列交叉验证来评估模型。
安装
pip install moda
用法
将原始数据集转换为moda数据集
moda使用MultiIndex来存储日期戳和类别。所有模型都已调整为接受这种结构。
假设输入数据集每行有一个条目,以及一个名为'date'的日期戳列。一个额外的'category'列是可选的。作为第一步,数据集被聚合到一个固定大小的时期间隔,并创建了一个新的数据集,包含'date'、'category'(可选)和'value'列。
'date'(pandas DatetimeIndex)和'category'的多级索引是数据集的索引。
import pandas as pd
from moda.dataprep import raw_to_ts, ts_to_range
raw_data_path = f"example/SF_data/SF311-2008.csv"
# The full dataset can be downloaded from here:
# https://data.sfgov.org/City-Infrastructure/311-Cases/vw6y-z8j6/data
TIME_RANGE = f"24H" # Aggregate all events in the raw data into 24 hours intervals
# Read raw file
raw = pd.read_csv(raw_data_path)
# Turn the raw data into a time series (with date as a pandas DatetimeIndex)
ts = raw_to_ts(raw)
# Aggregate items per time and category, given a time interval
ranged_ts = ts_to_range(ts=ts,time_range=TIME_RANGE)
模型评估
from moda.evaluators import get_metrics_for_all_categories, get_final_metrics
from moda.dataprep import read_data
from moda.models import STLTrendinessDetector
dataset = read_data(f"datasets/SF24H_labeled.csv")
print(dataset.head())
model = STLTrendinessDetector(freq=f'24H',
min_value=10,
anomaly_type=f'residual',
num_of_std=3, lo_delta=0)
# Take the entire time series and evaluate anomalies
# on all of it or just the last window(s)
prediction = model.predict(dataset)
raw_metrics = get_metrics_for_all_categories(dataset[['value']],
prediction[['prediction']],
dataset[['label']],
window_size_for_metrics=1)
metrics = get_final_metrics(raw_metrics)
print(f"f1 = {metrics['f1']}")
print(f"precision = {metrics['precision']}")
print(f"recall = {metrics['recall']}")
## Plot results for each category
#model.plot(labels=dataset['label'])
示例
此示例的jupyter笔记本可以在此处找到:这里。
一个更详细的示例,其中包括探索性数据分析,可以在此处找到:这里。
当前包含的模型
-
基于移动平均的季节性分解(针对趋势检测优化的MA)
对statsmodel的seasonal_decompose的包装。一种简单的分解,使用移动平均来移除趋势,并使用卷积滤波器来检测季节性。结果是残差时间序列。
为了在时间序列中检测异常和有趣的趋势,我们在分解的趋势序列和残差序列中寻找异常值。如果某个点的值高于先前窗口中历史值的多个标准差,则认为该点为异常值。我们评估了不同的趋势预测策略
- 仅残差异常
- 仅趋势异常
- 残差或趋势异常
- 残差与趋势异常
这是基线模型,在季节性基本恒定时可以得到不错的结果。
-
使用Loess进行季节性和趋势分解(改进的STL)
STL使用迭代Loess平滑来估计趋势,然后再次使用Loess平滑来提取变化的加性季节成分。它可以处理任何类型的季节性,季节性值可以随时间变化。我们使用了与基于移动平均的季节性分解相同的异常检测机制。在https://github.com/jrmontag/STLDecompose上使用此模型,当趋势和季节性具有更复杂的模式时。它通常优于移动平均模型。
STL的示例输出图:
左侧显示原始(顶部)和分解的时间序列(季节性、趋势、残差)。右侧显示残差时间序列上发现的异常(顶部)、趋势、预测(残差和趋势异常的组合)和真实值(底部)。
-
Azure异常检测器
将Azure Anomaly Detector认知服务作为检测异常的黑盒使用。Azure Anomaly finder提供了一个上限,可以用来估计异常的程度。当异常具有相对复杂结构时,此模型很有用。
-
Twitter
Twitter的AnomalyDetection包的包装器(https://github.com/Marcnuth/AnomalyDetection)此模型类似于(1)和(2),但在分析时间序列后具有更复杂的异常检测方式。
-
LSTMs
训练预测LSTM模型,比较时间t的预测值与时间t的实际值。然后,通过比较以前差异的标准差来估计差异。这仅在存在足够数据表示时间序列模式时才有用。
可以在这里找到运行LSTMs的示例。
运行测试和linting
Moda
使用pytest进行测试。要运行测试,只需从Moda
的主目录中调用pytest
。对于linting,此模块使用PEP8约定。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
构建分发
moda-0.3.3-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f5c45a6e57d0db5abd51da4a4ad29ad65cd554ca5e123d351b3d68eb2c4cc15c |
|
MD5 | 5fce0f518854d37bc18992a4283524d7 |
|
BLAKE2b-256 | 8b1c95c3e8ac709ca2f182e61008c128c77aa62246e884c7a09c0df63cfaecb1 |