如何在数据框的每一列中搜索异常?

问题描述

我有一个数据框,我的目标是为每个不同的列查找异常。因此,我正在寻找单变量异常。

假设这是我的数据框:

Dir()

我面临两个问题:

  1. 哪种算法足以实现此目标?例如。隔离森林?
  2. 如何在所有列上运行算法(例如隔离林),而不是逐列地执行算法?我可以使用for循环吗?

感谢您的帮助!

解决方法

第二季度:例如

df = pd.DataFrame({"bytes":[1,2,3,4,5],"flow":[1,"userid":[1,5]}).set_index("userid")

def get_anomaly(arr):
    if arr.bytes < 3 and arr.flow < 3:
        return -1
    elif arr.bytes > 3 and arr.flow > 3:
        return 1
    else:
        return 0

df['is_anomaly'] = df.apply(get_anomaly,axis=1)

>>> df
   bytes  flow  userid  is_anomaly
0      1     1       1       -1
1      2     2       2       -1
2      3     3       3        0
3      4     4       4        1
4      5     5       5        1

我们可以谈谈第一季度。

0级:线性关系或其他经历

Box-plot: min outlier < Q1-1.5ΔQ <= normal data <= Q3+1.5ΔQ < max outlier

Scott rule: Δb=3.5σn1/3 .Split the data and do distribution statistics

Other data status: avg. mean std and so on.

第1级:统计算法

Great algo: 
CMP
https://www.sciencedirect.com/science/article/abs/pii/S1389128616301633

Beehive
https://nds2.ccs.neu.edu/papers/Beehive.pdf

CBLOF
https://www.goldiges.de/publications/Anomaly_Detection_Algorithms_for_RapidMiner.pdf

And some AR MA ARMA algo,I don't know much.

第2级:无监督学习

Kmeans and so on...(This is actually quite a lot)

第3级:有监督的学习

from elasticsearch (doc)

EWMA  
s2=α*x2+(1-α)*s1

Holt-Linear  
s2=α*x2+(1-α)*(s1+t1)
t2=ß*(s2-s1)+(1-ß)*t1

Holt-Winters
si=α(xi-pi-k)+(1-α)(si-1+ti-1)
ti=ß(si-si-1)+(1-ß)ti-1
pi=γ(xi-si)+(1-γ)pi-k

from ML
CNN RNN LSTM Prefixspan AutoML Bayes and so on.(There are a few scenarios you can use.)

有太多未列出的东西,太多的算法要使用,太多的适当的信息,太多的细节要写下来。 UEBA的思想在分析异常时很重要。