forked from Bhargav5/PAIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROC_AUC.py
178 lines (144 loc) · 5.26 KB
/
ROC_AUC.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import IsolationForest
from scipy import stats
import PAA as paa
import time
# Reading data from the text file
def file_to_list(file_name, ts_len):
f1 = open(file_name,'r')
file_data = []
for line in f1:
line = line.strip()
ip_val = float(line)
file_data.append(ip_val)
f1.close()
ts_list = []
for x in range(0, len(file_data), ts_len):
ts_list.append(file_data[x:x+ts_len])
return ts_list
def plot_graphs(ref_list, test_list, anomaly_score, predict):
plt.subplot(411)
for x in ref_list:
plt.plot(range(len(x)), x)
plt.title("Training Signal")
plt.xlabel("Time")
plt.ylabel("Value")
plt.subplot(412)
for y in test_list:
plt.plot(range(len(y)), y)
plt.title("Test Signal")
plt.xlabel("Time")
plt.ylabel("Value")
#plt.xticks([x for x in range(0, len(test_list),100)])
plt.subplot(413)
plt.plot(range(len(anomaly_score)),anomaly_score)
#plt.xticks([x for x in range(0, len(test_list), 100)])
plt.title("Anomaly Score")
plt.xlabel("Sequence Number")
plt.ylabel("Value")
#plt.ylim([-1.2,1.2])
plt.subplot(414)
plt.plot(range(len(predict)),predict)
plt.ylim([-1.2, 1.2])
# plt.xticks([x for x in range(0, len(test_list), 100)])
plt.title("Detected Anomalies")
plt.xlabel("Sequence Number")
plt.ylabel("Value")
plt.show()
def predict (anomaly_score, contamination):
anomaly_score = np.array(anomaly_score)
print ("Anomaly_score_shape = {}".format(anomaly_score.shape[0]))
contamination = float(contamination)
print ("Contamination = {}".format(contamination))
threshold = stats.scoreatpercentile(anomaly_score, 100 * (contamination))
print ("Threshold = {}".format(threshold))
is_inlier = np.ones(anomaly_score.shape[0], dtype=int)
is_inlier[anomaly_score <= threshold] = -1
return is_inlier
# Main script starts
training_ts_list = file_to_list('multivariant_training7_1359.txt', 1359)
testing_ts_list = file_to_list('multivariant_testing7_1584.txt', 1584)
print (len(training_ts_list[0]), len(training_ts_list))
print (len(testing_ts_list[0]), len(testing_ts_list))
w = 30
m = 10
st = time.time()
training_paa = paa.ts_to_PAA(w,m,training_ts_list)
testing_paa = paa.ts_to_PAA(w,m,testing_ts_list)
print ("PAA time = {}".format(time.time() - st))
#print (training_paa)
print (training_paa.shape)
t1 = time.time()
IF1 = IsolationForest(max_samples= 256, n_estimators=100, contamination= 0.01)
IF1.fit(training_paa)
#cont = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5]
cont = [0.01, 0.02,0.04,0.08,0.1, 0.2,0.4, 0.5]
for val in cont:
anomaly_score = IF1.decision_function(testing_paa)
anomaly_score = [0 for x in range(w)] + [z for z in anomaly_score]
predict_score = predict(anomaly_score, val)
predict_socre = [z for z in predict_score]
plot_graphs(training_ts_list, testing_ts_list, anomaly_score, predict_score)
plt.subplot(411)
plt.title('Training Signal')
plt.xlabel('Instance Number')
plt.ylabel('Value')
plt.plot(range(len(training_ts_list[0])), training_ts_list[0], color='b')
plt.plot(range(len(training_ts_list[1])), training_ts_list[1], color='r')
plt.plot(range(len(training_ts_list[2])), training_ts_list[2], color='g')
plt.subplot(412)
plt.title('Testing Signal')
plt.xlabel('Instance Number')
plt.ylabel('Value')
plt.plot(range(len(testing_ts_list[0])), testing_ts_list[0], color='b')
plt.plot(range(len(testing_ts_list[1])), testing_ts_list[1], color='r')
plt.plot(range(len(testing_ts_list[2])), testing_ts_list[2], color='g')
plt.subplot(413)
plt.title('Anomaly Score')
plt.xlabel('Instance Number')
plt.ylabel('Anomaly Score')
plt.plot(range(len(anomaly_score)), anomaly_score)
plt.subplot(414)
plt.plot(range(len(predict_score)), predict_score)
plt.ylim([-1.2,1.2])
plt.show()
plt.close()
'''
# Plots for multivariant series paper results
plt.subplot(411)
plt.plot(range(len(testing_ts_list[0])), testing_ts_list[0])
plt.title('Signal 1')
plt.subplot(412)
plt.plot(range(len(testing_ts_list[1])), testing_ts_list[1])
plt.title('Signal 2')
#plt.subplot(513)
#plt.plot(range(len(testing_ts_list[2])), testing_ts_list[2])
#plt.title('Signal 3')
plt.subplot(413)
plt.plot(range(len(anomaly_score)), anomaly_score)
plt.title('Anomaly Score')
plt.subplot(414)
plt.plot(range(len(predict_score)), predict_score)
plt.title('Predict')
plt.show()
'''
'''
# Plots for paper results
plt.subplot(511)
plt.plot(range(len(testing_ts_list[0])),testing_ts_list[0])
plt.title('Signal 1')
plt.subplot(512)
plt.plot(range(len(testing_ts_list[1])),testing_ts_list[1])
plt.title('Signal 2')
plt.subplot(513)
plt.plot(range(len(testing_ts_list[2])),testing_ts_list[2])
plt.title('Signal 3')
plt.subplot(514)
plt.plot(range(len(anomaly_score)),anomaly_score)
plt.title('Anomaly Score')
plt.subplot(515)
plt.plot(range(len(predict)),predict)
plt.title('Anomaly Score')
plt.show()
'''