-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnomalyDetection.py
More file actions
129 lines (81 loc) · 2.4 KB
/
AnomalyDetection.py
File metadata and controls
129 lines (81 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import xlrd
import xlwt
from xlutils.copy import copy
import numpy as np
from math import exp, pi
workbook = xlrd.open_workbook("训练数据.xlsx")
sheet = workbook.sheet_by_name("Sheet5")
H = []
W = []
L = []
for i in range(1,sheet.nrows):
H.append(sheet.cell(i,3).value)
W.append(sheet.cell(i,5).value)
L.append(sheet.cell(i,4).value)
# 均值
u_L = sum(L)/len(L)
u_W = sum(W)/len(W)
u_H = sum(H)/len(H)
mu = np.array([u_H, u_L, u_W])
# 训练样本集合
x = np.array([list(i) for i in zip(*[H,L,W])])
# 协方差矩阵
z = np.zeros((3,3))
for i in range(len(H)):
a = x[i].reshape(3,1)
b = x[i].reshape(1,3)
z += np.matmul(a,b)
z = z/len(H)
# 验证数据集
# 主要错误形式
# 1.高度太高或者太低
# 2.截面高宽不匹配
va = np.array([[1,2000,2000],
[30,2000,2000],
[10,4000,1000],
[10,1000,4000]])
label_1 = np.zeros((len(x),1))
label_2 = np.ones((len(va),1))
# 整合数据集
data = np.append(x,va,axis=0)
labels = np.append(label_1, label_2,axis=0)
ep, f = epSelection(data, labels)
# 定义概率计算函数
def p(x,mu,z):
# x 是新样本,应为n维的数据。n=len(mu)
# mu : 训练样本均值
# z : 训练样本协方差矩阵
n = len(x)
# 系数
a = 1/((2*pi)**(n/2)*(np.linalg.det(z))**2)
r = -0.5 * np.matmul(np.matmul((x-mu).reshape(1,n), np.linalg.inv(z)), (x-mu).reshape(n,1))
return a * exp(r)
# 挑选最合适的 ep
def epSelection(data,labels):
dict = {}
for i in range(10,100):
ep = i*10**(-31)
label_p = []
tp = 0
fn = 0
fp = 0
for i in range(len(data)):
if p(data[i],mu,z)<ep:
label_p.append(1)
else:
label_p.append(0)
# 得到算法预测的 label_p,计算 tp,fn,fp
for i in range(len(label_p)):
if label_p[i]==0 and labels[i]==0:
tp += 1
elif label_p[i]==0 and labels[i]==1:
fn += 1
elif label_p[i]==1 and labels[i]==0:
fp += 1
# prep, rec
prep = tp/(tp+fp)
rec = tp/(tp+fn)
F1 = 2*prep*rec/(prep+rec)
dict[ep] = F1
pair = sorted(dict.items(), key=lambda x:x[1])[-1]
return pair[0], pair[1]