output_matrix.py
Go to the documentation of this file.
1 '''
2 Ensemble Anomaly Detector Framework
3 
4 Author:
5  Vedanth Narayanan
6 File:
7  Output Matrix class
8 Date:
9  8 May, 2018
10 
11 '''
12 
13 import numpy as np
14 from sklearn.metrics import precision_score
15 from sklearn.metrics import recall_score
16 from sklearn.metrics import f1_score
17 
18 
19 class OutputMatrix(object):
20 
21  def __init__(self):
22  self.__tp = None
23  self.__fp = None
24  self.__tn = None
25  self.__fn = None
26  self.__acc = None
27  self.__rec = None
28  self.__prec = None
29  self.__f1 = None
30 
31 
32  def set_confusion_matrix(self, tp, fp, tn, fn):
33  '''
34  First to be called.
35  Sets the confusion matrix values.
36  '''
37 
38  self.__tp = tp
39  self.__fp = fp
40  self.__tn = tn
41  self.__fn = fn
42 
43 
44  def set_measures(self):
45  '''
46  Before measuers are set, make sure confusion matrix
47  exists, or else exit.
48  '''
49 
50  if self.__tp is None or self.__fp is None or \
51  self.__tn is None or self.__fn is None:
52  print 'Error: Confusion matrix values not set.'
53  return
54 
55  self.set_accuracy()
56  self.set_recall()
57  self.set_precision()
58  self.set_f1measure()
59 
60 
61  def get_measures(self):
62  '''
63  Measures are printed and returned.
64  '''
65 
66  acc = self.get_accuracy()
67  rec = self.get_recall()
68  prec = self.get_precision()
69  f1 = self.get_f1measure()
70 
71  print '\taccuracy', acc
72  print '\trecall', rec
73  print '\tprecision', prec
74  print '\tf1measure', f1
75 
76  return acc, rec, prec, f1
77 
78 
79  def get_accuracy(self):
80  return self.__acc
81 
82 
83  def get_recall(self):
84  return self.__rec
85 
86 
87  def get_precision(self):
88  return self.__prec
89 
90 
91  def get_f1measure(self):
92  return self.__f1
93 
94 
95  def set_accuracy(self):
96  '''
97  acc = (tp + fp) / (tp + fp + tn + fn)
98  '''
99 
100  res = float(self.__tp + self.__tn) / \
101  (self.__tp + self.__fp + self.__tn + self.__fn + 1e-6)
102  self.__acc = res
103 
104 
105  def set_recall(self):
106  '''
107  rec = tp / (tp + fn)
108 
109 
110  '''
111 
112  res = float(self.__tp + 1e-6) / (self.__tp + self.__fn + 1e-6)
113  self.__rec = res
114 
115 
116  def set_precision(self):
117  '''
118  prec = tp / (tp + fp)
119 
120 
121  '''
122 
123  res = float(self.__tp + 1e-6) / (self.__tp + self.__fp + 1e-6)
124  self.__prec = res
125 
126 
127  def set_f1measure(self):
128  '''
129  f1 = (2 * rec * prec) / (rec + prec)
130 
131  Balanced F1 score that is the harmonic mean of both recall
132  and precision.
133  '''
134 
135  res = float(2*self.__rec*self.__prec) / (self.__rec+self.__prec)
136  self.__f1 = res
137 
138 
139  def output_matrix(self, true_y, pred_y):
140  '''
141  Output matrix
142  Meant to print the output confusion matrix. Specifically
143  - True Positive
144  - False Positive
145  - True Negative
146  - False Negative
147 
148  The above gets printed for both the training data and testing data.
149  '''
150 
151  count_train = len(true_y)
152  tpos = fpos = tneg = fneg = 0
153  for i in xrange(count_train):
154  # print pred_y[i], true_y[i, 0]
155  if (pred_y[i] == -1) and (true_y[i, 0] == -1):
156  tpos += 1
157  elif (pred_y[i] == -1) & (true_y[i, 0] == 1):
158  fpos += 1
159  elif (pred_y[i] == 1) & (true_y[i, 0] == 1):
160  tneg += 1
161  elif (pred_y[i] == 1) & (true_y[i, 0] == -1):
162  fneg += 1
163 
164  benign = float(fneg + tneg) / count_train
165  attack = float(tpos + fpos) / count_train
166 
167  tpos = float(tpos) / count_train
168  fpos = float(fpos) / count_train
169  tneg = float(tneg) / count_train
170  fneg = float(fneg) / count_train
171 
172  self.set_confusion_matrix(tpos, fpos, tneg, fneg)
173  self.set_measures()
174 
175  print 'Benign/Attack:\t', "{:.4f}".format(benign), '\t', "{:.4f}".format(attack)
176  print 'Output Results - '
177  print '\ttpos: \t', "{:.4f}".format(tpos)
178  print '\tfpos: \t', "{:.4f}".format(fpos)
179  print '\ttneg: \t', "{:.4f}".format(tneg)
180  print '\tfneg: \t', "{:.4f}".format(fneg)
181  # print '\tleft: ', "{:.3f}".format(left)
182  print '\taccuracy: \t', "{:.4f}".format(self.get_accuracy())
183  print '\trecall: \t', "{:.4f}".format(self.get_recall())
184  print '\tprecision: \t', "{:.4f}".format(self.get_precision())
185  print '\tf1measure: \t', "{:.4f}".format(self.get_f1measure())
186 
187  # if attack > 0.22:
188  # print '\tDETECT'
189  # else:
190  # print '\tBenign'
191 
192  print ''
def set_confusion_matrix(self, tp, fp, tn, fn)
def output_matrix(self, true_y, pred_y)


mh5_anomaly_detector
Author(s): Vedanth Narayanan
autogenerated on Mon Jun 10 2019 13:49:20