利用RRCF包进行异常值检测

首先准备数据:

import rrcf
df.head(10)

准备数据,去掉时间列

# 准备数据,去掉时间列
X = df.drop(columns=['dtime']).values
num_trees = 100
tree_size = 256
forest = []

# 存储每个点的索引以便之后计算CoDisp
indices = {}

for _ in range(num_trees):
    ixs = np.random.choice(len(X), size=tree_size, replace=False)
    tree = rrcf.RCTree()
    for ix in ixs:
        index = (ix, _)
        tree.insert_point(X[ix], index=index)
        if index not in indices:
            indices[index] = []
        indices[index].append(tree)
    forest.append(tree)

# 计算一致偏离度(CoDisp)
scores = np.zeros(len(X))

for ix in range(len(X)):
    total_codisp = 0
    for tree in forest:
        if (ix, _) in tree.leaves:
            codisp = tree.codisp((ix, _))
            total_codisp += codisp
    scores[ix] = total_codisp / num_trees

# 将分数添加到原始数据中
df['anomaly_score'] = scores

结果可视:

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
plt.plot(df['dtime'], df['anomaly_score'], label='Anomaly Score')
plt.xlabel('Time')
plt.ylabel('Anomaly Score')
plt.title('Anomaly Scores Over Time')
plt.legend()
plt.show()

进一步可视化,方便做对比。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值