异常检测
Posted coshaho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异常检测相关的知识,希望对你有一定的参考价值。
import pandas as pd
import numpy as np
from pandas import Series
from pandas import DataFrame
import matplotlib.pyplot as plt
df = pd.read_csv(r‘indicator.csv‘, sep=‘,‘)
df = df.fillna(method=‘ffill‘)
# 自相关系数 rk = yk/y0
u = df[‘indicator‘].mean()
s1 = df[‘indicator‘][: -24]
s2 = df[‘indicator‘][24:]
s1 = s1 - u
s2 = s2 - u
# 索引对应相乘
s0 = s1 * s1
# 矩阵对应位置相乘
sk = np.multiply(s1, s2)
y0 = sum(s0)
yk = sum(sk)
rk = yk / y0
#print(rk)
from sklearn import svm
# reshape(-1, 1)矩阵转化为一列
data = np.array(df[‘indicator‘]).reshape(-1, 1)
algorithm = svm.OneClassSVM(nu=0.15, kernel=‘rbf‘, gamma=0.1)
model = algorithm.fit(data)
print(model)
pre_y = model.predict(data)
#print(df.shape)
df1 = df.copy()
df1[‘time‘] = range(120)
df1[‘clazz‘] = pre_y
#print(df1)
df2 = df1[df1[‘clazz‘] == 1]
df3 = df1[df1[‘clazz‘] == -1]
plt.figure()
plt.scatter(df2[‘time‘], df2[‘indicator‘])
plt.scatter(df3[‘time‘], df3[‘indicator‘])
plt.show()
time,indicator
2018-11-02-02 00:00:00,1
2018-11-02-02 01:00:00,2
2018-11-02-02 02:00:00,3
2018-11-02-02 03:00:00,4
2018-11-02-02 04:00:00,5
2018-11-02-02 05:00:00,6
2018-11-02-02 06:00:00,7
2018-11-02-02 07:00:00,8
2018-11-02-02 08:00:00,9
2018-11-02-02 09:00:00,10
2018-11-02-02 10:00:00,11
2018-11-02-02 11:00:00,12
2018-11-02-02 12:00:00,12
2018-11-02-02 13:00:00,11
2018-11-02-02 14:00:00,10
2018-11-02-02 15:00:00,9
2018-11-02-02 16:00:00,8
2018-11-02-02 17:00:00,7
2018-11-02-02 18:00:00,6
2018-11-02-02 19:00:00,5
2018-11-02-02 20:00:00,4
2018-11-02-02 21:00:00,3
2018-11-02-02 22:00:00,2
2018-11-02-02 23:00:00,1
2018-11-02-03 00:00:00,1
2018-11-02-03 01:00:00,2
2018-11-02-03 02:00:00,3
2018-11-02-03 03:00:00,4
2018-11-02-03 04:00:00,5
2018-11-02-03 05:00:00,6
2018-11-02-03 06:00:00,7
2018-11-02-03 07:00:00,8
2018-11-02-03 08:00:00,9
2018-11-02-03 09:00:00,10
2018-11-02-03 10:00:00,11
2018-11-02-03 11:00:00,12
2018-11-02-03 12:00:00,12
2018-11-02-03 13:00:00,11
2018-11-02-03 14:00:00,10
2018-11-02-03 15:00:00,9
2018-11-02-03 16:00:00,8
2018-11-02-03 17:00:00,7
2018-11-02-03 18:00:00,6
2018-11-02-03 19:00:00,5
2018-11-02-03 20:00:00,4
2018-11-02-03 21:00:00,3
2018-11-02-03 22:00:00,2
2018-11-02-03 23:00:00,1
2018-11-02-04 00:00:00,1
2018-11-02-04 01:00:00,2
2018-11-02-04 02:00:00,3
2018-11-02-04 03:00:00,4
2018-11-02-04 04:00:00,5
2018-11-02-04 05:00:00,6
2018-11-02-04 06:00:00,7
2018-11-02-04 07:00:00,8
2018-11-02-04 08:00:00,9
2018-11-02-04 09:00:00,10
2018-11-02-04 10:00:00,11
2018-11-02-04 11:00:00,12
2018-11-02-04 12:00:00,12
2018-11-02-04 13:00:00,11
2018-11-02-04 14:00:00,10
2018-11-02-04 15:00:00,9
2018-11-02-04 16:00:00,8
2018-11-02-04 17:00:00,7
2018-11-02-04 18:00:00,6
2018-11-02-04 19:00:00,5
2018-11-02-04 20:00:00,4
2018-11-02-04 21:00:00,3
2018-11-02-04 22:00:00,2
2018-11-02-04 23:00:00,1
2018-11-02-05 00:00:00,1
2018-11-02-05 01:00:00,2
2018-11-02-05 02:00:00,3
2018-11-02-05 03:00:00,4
2018-11-02-05 04:00:00,5
2018-11-02-05 05:00:00,6
2018-11-02-05 06:00:00,7
2018-11-02-05 07:00:00,8
2018-11-02-05 08:00:00,9
2018-11-02-05 09:00:00,10
2018-11-02-05 10:00:00,11
2018-11-02-05 11:00:00,12
2018-11-02-05 12:00:00,12
2018-11-02-05 13:00:00,11
2018-11-02-05 14:00:00,10
2018-11-02-05 15:00:00,9
2018-11-02-05 16:00:00,8
2018-11-02-05 17:00:00,7
2018-11-02-05 18:00:00,6
2018-11-02-05 19:00:00,5
2018-11-02-05 20:00:00,4
2018-11-02-05 21:00:00,3
2018-11-02-05 22:00:00,2
2018-11-02-05 23:00:00,1
2018-11-02-06 00:00:00,1
2018-11-02-06 01:00:00,2
2018-11-02-06 02:00:00,3
2018-11-02-06 03:00:00,4
2018-11-02-06 04:00:00,5
2018-11-02-06 05:00:00,6
2018-11-02-06 06:00:00,7
2018-11-02-06 07:00:00,8
2018-11-02-06 08:00:00,9
2018-11-02-06 09:00:00,10
2018-11-02-06 10:00:00,11
2018-11-02-06 11:00:00,12
2018-11-02-06 12:00:00,12
2018-11-02-06 13:00:00,11
2018-11-02-06 14:00:00,10
2018-11-02-06 15:00:00,9
2018-11-02-06 16:00:00,8
2018-11-02-06 17:00:00,7
2018-11-02-06 18:00:00,6
2018-11-02-06 19:00:00,5
2018-11-02-06 20:00:00,4
2018-11-02-06 21:00:00,3
2018-11-02-06 22:00:00,2
2018-11-02-06 23:00:00,1
以上是关于异常检测的主要内容,如果未能解决你的问题,请参考以下文章
异常检测——从经典算法到深度学习》14 对于流数据基于 RRCF 的异常检测
异常检测——从经典算法到深度学习》14 对于流数据基于 RRCF 的异常检测
异常检测——从经典算法到深度学习》14 对于流数据基于 RRCF 的异常检测