-
Notifications
You must be signed in to change notification settings - Fork 1
/
number_anomaly_detector.py
executable file
·190 lines (149 loc) · 5.89 KB
/
number_anomaly_detector.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
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
# This file is part of the Stratosphere Linux IPS
# See the file 'LICENSE' for copying permission.
# Authors:
# - Sebastian Garcia. eldraco@gmail.com,
# sebastian.garcia@agents.fel.cvut.cz
import pandas as pd
# from sklearn.model_selection import train_test_split
# from pyod.models import lof
# from pyod.models.abod import ABOD
# from pyod.models.cblof import CBLOF
# from pyod.models.lof import LOF
# from pyod.models.loci import LOCI
# from pyod.models.lscp import LSCP
# from pyod.models.mcd import MCD
# from pyod.models.ocsvm import OCSVM
from pyod.models.pca import PCA
# from pyod.models.sod import SOD
# from pyod.models.so_gaal import SO_GAAL # Needs keras
# from pyod.models.sos import SOS # Needs keras
# from pyod.models.xgbod import XGBOD # Needs keras
# from pyod.models.knn import KNN # kNN detector
import argparse
import warnings
import sys
import fileinput
# This horrible hack is only to stop sklearn from printing those warnings
def warn(*args, **kwargs):
pass
warnings.warn = warn
def detect(input_data, amountanom, window_size):
"""
Parent function to deal with real time or not
input_data: can be a file to open of STDIN
"""
if args.verbose:
print('Detecting')
if input_data:
if args.verbose:
print('By file')
# Create a Pandas dataframe from the conn.log
df = pd.read_csv(input_data, names=['values'])
detect_numbers(df, amountanom)
else:
if args.verbose:
print('Realtime')
# Read in groups of 'window_size' width and train and test on them
read_lines = 0
lines = []
try:
for line in iter(sys.stdin.readline, b''):
if line:
line = line.strip()
lines.append(line)
read_lines += 1
if read_lines == window_size:
print(f'Read new numbers. Processing...')
df = pd.DataFrame(lines, columns=['values'])
detect_numbers(df, amountanom)
read_lines = 0
else:
break
# Capture the last batch
df = pd.DataFrame(lines, columns=['values'])
detect_numbers(df, amountanom)
except KeyboardInterrupt:
sys.stdout.flush()
def detect_numbers(df, amountanom):
"""
Function to apply a very simple anomaly detector to a set of numbers given as a pandas dataframe
amountanom: The top number of anomalies we want to print
df: input dataframe with numbers
"""
# Replace the rows without data (with '-') with 0.
df['values'].replace('-', '0', inplace=True)
# Add the columns from the log file that we know are numbers. This is only for conn.log files.
X_train = df[['values']]
# The X_test is where we are going to search for anomalies. In our case, its the same set of data than X_train.
X_test = X_train
#################
# Select a model from below
# ABOD class for Angle-base Outlier Detection. For an observation, the
# variance of its weighted cosine scores to all neighbors could be
# viewed as the outlying score.
# clf = ABOD()
# LOF
# clf = LOF()
# CBLOF
# clf = CBLOF()
# LOCI
# clf = LOCI()
# LSCP
# clf = LSCP()
# MCD
# clf = MCD()
# OCSVM
# clf = OCSVM()
# PCA. Good and fast!
clf = PCA()
# SOD
# clf = SOD()
# SO_GAAL
# clf = SO_GALL()
# SOS
# clf = SOS()
# XGBOD
# clf = XGBOD()
# KNN
# Good results but slow
# clf = KNN()
# clf = KNN(n_neighbors=10)
#################
# Fit the model to the train data
clf.fit(X_train)
# get the prediction on the test data
y_test_pred = clf.predict(X_test) # outlier labels (0 or 1)
y_test_scores = clf.decision_function(X_test) # outlier scores
# Convert the ndarrays of scores and predictions to pandas series
scores_series = pd.Series(y_test_scores)
pred_series = pd.Series(y_test_pred)
# Now use the series to add a new column to the X test
X_test['score'] = scores_series.values
X_test['pred'] = pred_series.values
# Add the score to the df also. So we can show it at the end
df['score'] = X_test['score']
# Keep the positive predictions only. That is, keep only what we predict is an anomaly.
X_test_predicted = X_test[X_test.pred == 1]
# Keep the top X amount of anomalies
top10 = X_test_predicted.sort_values(by='score', ascending=False).iloc[:amountanom]
# Print the results
# Find the predicted anomalies in the original df dataframe, where the rest of the data is
df_to_print = df.iloc[top10.index]
print('\nTop anomalies')
# Only print some columns, not all, so its easier to read.
#df_to_print = df_to_print.drop(['values'], axis=1)
# Dont print index
print(df_to_print.to_string(index=False))
if __name__ == '__main__':
print('Simple Number Anomaly Detector. Version: 0.1')
print('Author: Sebastian Garcia (eldraco@gmail.com)')
# Parse the parameters
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', help='Amount of verbosity. This shows more info about the results.', action='store', required=False, type=int)
parser.add_argument('-e', '--debug', help='Amount of debugging. This shows inner information about the program.', action='store', required=False, type=int)
parser.add_argument('-f', '--file', help='Path to the input file to read.', required=False)
parser.add_argument('-a', '--amountanom', help='Amount of anomalies to show.', required=False, default=10, type=int)
parser.add_argument('-w', '--window_size', help='Width of the groups of numbers to read and detect if using STDIN.', required=False, type=bool, default=10)
args = parser.parse_args()
detect(args.file, args.amountanom, window_size=args.window_size)