寻找数百万记录的异常

发布于 2025-02-08 14:57:50 字数 2421 浏览 2 评论 0原文

我有一个包含4列的数据库(超过2m行):

  1. PC
  2. 用户
  3. 日期
  4. 计数

“计数”列是(PC +用户 + date)的汇总基础 t是多少特定用户在特定日期访问特定的计算机 :例如:

PC用户日期计数
A2020-01-015B
AA2020-01-028
AB2020-02-045
BB2020-01-015
BC2020-02-045

因此 第一: 用户在2020-01-01中连接到PC的PC连接了5次 等等。...

我创建了一种算法,该算法采用了所有“组合”并计算隔离林

def isolationForest_group(group_count):
    scaler = standardscaler()
    np_scaler = scaler.fit_transform(group_count.values.reshape(-1,1)
    data = pd.DataFrame(np_scaler)
    model = IsolationForest()
    model.fir(data)
    return model.predict(data)

df['Anomaly_ISO') = df.groupby(['PC','USER'])['Count'].transform(isolationForest_group)

但它不有效,有一种方法可以使其变得更好(减少运行时间的时间)??? 该算法不必是隔离算法算法 最终结果应该看起来像:

PC用户日期计数异常
AA2020-01-0150
A A AA A A2020-01-0280
A AA2020-01-035001
AA A A A A A2020-01-0450
AA2020-02-0450
AB2020-02-0150
AB2020-02-02-028001
AB2020-02-0450
AB2020-03-0150
AB2020-03-- 0450
cA2020-01-0140
CA2020-01-021001
CA2020-01-0350

因此,首先,该算法将PC A和用户A的组合和所有日期和所有日期及所有日期及所有日期进行与用户B的组合A等等。

i have a database (more then 2M rows) that contains 4 columns:

  1. PC
  2. User
  3. Date
  4. Count

The 'Count' columns is aggregate base on (PC + User + Date)
t's how many specific user visit specific computer in specific day
So for example:

PCUSERDateCount
Aa2020-01-015
Aa2020-01-028
Ab2020-02-045
Bb2020-01-015
Bc2020-02-045

For row number one:
User a Connected to PC A 5 time in 2020-01-01
etc....

i created an algorithm that take every ' combination' and calculate the isolation forest

def isolationForest_group(group_count):
    scaler = standardscaler()
    np_scaler = scaler.fit_transform(group_count.values.reshape(-1,1)
    data = pd.DataFrame(np_scaler)
    model = IsolationForest()
    model.fir(data)
    return model.predict(data)

df['Anomaly_ISO') = df.groupby(['PC','USER'])['Count'].transform(isolationForest_group)

but its not Efficient there is a way to make it better (Reduce the time it takes to run )???
The algorithm does not have to be a isolationForest algorithm
the end result should look like that:

PCUSERDateCountAnomaly
Aa2020-01-0150
Aa2020-01-0280
Aa2020-01-035001
Aa2020-01-0450
Aa2020-02-0450
Ab2020-02-0150
Ab2020-02-028001
Ab2020-02-0450
Ab2020-03-0150
Ab2020-03-0450
Ca2020-01-0140
Ca2020-01-021001
Ca2020-01-0350

So first the algorithm take ALL dates for the combination of PC A and User a and All Dates for combination A with user b and so on....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2025-02-15 14:57:50

我可以提出统计方法,而不是ML,它起作用非常快,但是我不知道它是否对您有用。您可以使用 boxplots 进行异常检测)中的相同公式。您可以通过增加或减少IQR乘数来设置该方法的灵敏度。因此,您的代码看起来像这样:

import pandas as pd
import numpy as np

#exporting your df
q1,q3 = np.quantile(df['Count'], [.25,.75])
iqr = q3 - q1

df['Anomaly'] = df['Count'] > (q3 + 1.5 *iqr)

结果:

I can propose the statistic method, not ML, it works really fast, but I don't know if it will work for you. You can use the same formula used in boxplots for anomaly detection. You can set the sensitivity of the method by increasing or decreasing IQR multiplier. So your code can look like this:

import pandas as pd
import numpy as np

#exporting your df
q1,q3 = np.quantile(df['Count'], [.25,.75])
iqr = q3 - q1

df['Anomaly'] = df['Count'] > (q3 + 1.5 *iqr)

The result:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文