-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
421 lines (342 loc) · 15.3 KB
/
models.py
File metadata and controls
421 lines (342 loc) · 15.3 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os
from copy import deepcopy
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, roc_curve, auc, precision_recall_fscore_support, \
precision_recall_curve, average_precision_score
import pandas as pd
import numpy as np
CSV_WALKING_PATH = os.path.split(__file__)[0] + '/Kyoto2007' # file path with default dataset
def fit_label_encoder(not_float):
number = LabelEncoder()
return number.fit_transform(not_float)
def name_correct(func):
def checking_wrapper(self, name, *args, **kwargs):
if not isinstance(name, str):
raise ValueError('Name of model should have type "str"')
result = func(self, name, *args, **kwargs)
return result
return checking_wrapper
class DataProcessor:
using_features = ['_DestinationPortNumber_', '_SourcePortNumber_', '_Service_', '_Count_', '_SerrorRate_',
'_DstHostCount_', '_DstHostSRVCount_', '_DstHostSameSRCPortRate_', '_SameSRVRate_',
'_Label_'] # последней всегда должны идти лейблы, если они есть
def __init__(self, name, attack_ratio=0.05, attack_label=-1, processing_mode=0,
data=None, using_features=None, csv_separator='\t',
num_classes=2, labeled_data=True):
"""
:param processing_mode:
0 - read data from files in given path (non-recursive), type(data) is str
1 - data is iterable
"""
self.name = name
self.attack_ratio = attack_ratio
self.attack_label = attack_label
if using_features is not None:
self.using_features = using_features
else:
if not labeled_data:
self.using_features.pop()
if data is None:
self.data = CSV_WALKING_PATH
self.processing_mode = 0
else:
self.processing_mode = processing_mode
self.data = data
self.csv_separator = csv_separator
self.num_classes = num_classes
self.labeled_data = labeled_data
self.__check_parameters_correctness()
if not self.processing_mode:
self.extract_data_from_path()
self.fetch_classes()
self.__make_all_features_numeric()
def fetch_classes(self):
if self.num_classes == 2:
self.data[self.using_features[-1]] = self.data[self.using_features[-1]].apply(
lambda x: self.attack_label if x * self.attack_label > 0 else x
)
def get_labels(self):
if self.labeled_data:
return np.array(self.data[self.using_features[-1]])
else:
raise AttributeError('Cant give labels from labelless dataset')
def get_features(self):
if self.labeled_data:
features_without_labels = deepcopy(self.using_features)
features_without_labels.remove(self.using_features[-1])
return np.array(self.data[features_without_labels])
return np.array(self.data[self.using_features])
def __make_all_features_numeric(self):
for index in self.data.columns.values:
for item in self.data[index]:
if item not in [0, '0', '0.0', []]:
try:
float(item)
except (ValueError, TypeError):
self.data[index] = fit_label_encoder(self.data[index])
break
def extract_data_from_path(self):
all_csv_files = []
df = pd.DataFrame()
for item in os.walk(self.data):
for f in item[-1]:
if f[-1:-5:-1][::-1] == '.csv':
all_csv_files.append('/'.join([item[0], f]))
for csv_file in all_csv_files:
df = pd.read_csv(csv_file, sep=self.csv_separator)
try:
df['_DurationOfConnection_']
except KeyError:
pass # its fine
else:
df = df.append(df)
indices = df[:-1]
self.data = indices[self.using_features]
def get_train_test_split(self, test_size=0.33):
if self.labeled_data:
return train_test_split(self.get_features(),
self.get_labels(),
test_size=test_size)
return train_test_split(self.get_features(),
test_size=test_size)
def __check_parameters_type_correctness(self):
if not isinstance(self.attack_ratio, (int, float)):
raise ValueError('Parameter attack_ratio should has type int or float')
try:
list(self.using_features)
except TypeError:
raise ValueError('Parameter attack_probability should be iterable')
try:
list(self.data)
except TypeError:
raise ValueError('Parameter training_data should be iterable')
if not isinstance(self.csv_separator, str):
raise ValueError('Parameter csv_separator should has type str')
def __check_parameters_value_correctness(self):
if not 0 < self.attack_ratio < 1:
raise ValueError('Parameter attack_ratio should be in range (0, 1)')
if str(self.attack_label) not in ('-1', '1', '-1.0', '1.0'):
raise ValueError('Parameter attack_label should be in set {-1, 1}')
self.attack_label = int(self.attack_label)
if str(self.processing_mode) not in ('0', '1', '0.0', '1.0'):
raise ValueError('Parameter processing_mode should be in set {0, 1}')
self.processing_mode = int(self.processing_mode)
def __check_parameters_consistency(self):
if self.processing_mode == 0:
if not isinstance(self.data, str):
raise ValueError('Parameter training_data should have type str when processing_mode == 0')
elif self.processing_mode == 1:
if isinstance(self.data, str):
raise ValueError('Parameter training_data should not have type str when processing_mode == 1')
def __check_parameters_correctness(self):
self.__check_parameters_type_correctness()
self.__check_parameters_value_correctness()
self.__check_parameters_consistency()
class Model:
train_features = None
test_features = None
train_labels = None
test_labels = None
def __init__(self, name, ml_algorithm, features, labels, parent, test_size=0.33, attack_label=-1):
self.name = name
self.ml_algorithm = ml_algorithm()
self.attack_label = attack_label
self.__check_ml_algorithm_correctness(ml_algorithm)
self.train_features, self.test_features, \
self.train_labels, self.test_labels = train_test_split(features,
labels,
test_size=test_size)
self.parent = parent
self.fit()
def __check_ml_algorithm_correctness(self, ml_algorithm):
try:
ml_algorithm.fit()
except AttributeError:
raise TypeError('Model "%s" has no fit method.' % self.name)
except TypeError:
pass
try:
ml_algorithm.predict()
except AttributeError:
raise TypeError('Model "%s" has no predict method.' % self.name)
except TypeError:
pass
try:
ml_algorithm.predict_proba()
except AttributeError:
print('Warning:\nModel "%s" has no predict_proba method.' % self.name)
except TypeError:
pass
def predict_proba(self, p_value=0.5, data=None):
print('Model named "%s" predicting proba' % self.name)
if data is None:
try:
predicted = self.ml_algorithm.predict_proba(self.test_features)
return np.apply_along_axis(
lambda x: 1 if x[0] > p_value else 0, 1, predicted # this mb a bit wrong
), self.test_labels
except AttributeError:
raise TypeError('Model "%s" has no predict_proba method' % self.name)
else:
return self.ml_algorithm.predict_proba(data), None
def fit(self):
print('Fitting model named "%s"' % self.name)
self.ml_algorithm.fit(self.train_features, self.train_labels)
def predict(self, data=None):
print('Model named "%s" predicting' % self.name)
if data is None:
data = self.test_features
return self.ml_algorithm.predict(data), self.test_labels
else:
if isinstance(data, str):
features = self.parent.data[data].get_features()
labels = self.parent.data[data].get_labels()
return self.ml_algorithm.predict(features), labels
return self.ml_algorithm.predict(data), None
class ModelsManager:
models = {}
data = {}
metrics = {}
def __init__(self):
self.data['kyoto2007'] = DataProcessor('kyoto2007')
self.__load_gradient_boosting()
self.__load_logistic_regression()
self.__add_default_metrics()
def __add_default_metrics(self):
default_metric_names = [
'average_precision_score',
'accuracy_score',
'roc_curve',
'auc',
'precision_recall_fscore_support',
'precision_recall_curve'
]
default_metrics = [
average_precision_score,
accuracy_score,
roc_curve,
auc,
precision_recall_fscore_support,
precision_recall_curve
]
for name, metric in zip(default_metric_names, default_metrics):
self.add_metric(name, metric)
@name_correct
def predict(self, model_name, data_name):
try:
return self.models[model_name].predict(self.data[data_name].get_features())
except KeyError:
if model_name in self.models.keys():
self.__data_not_found(data_name)
else:
self.__model_not_found(model_name)
@name_correct
def predict_proba(self, model_name, data_name):
try:
return self.models[model_name].predict_proba(self.data[data_name].get_features())
except KeyError:
if model_name in self.models.keys():
self.__data_not_found(data_name)
else:
self.__model_not_found(model_name)
def __data_not_found(self, data_name):
print('Dataset named "%s" doesnt exist' % data_name)
print('Available data sets:')
for data in self.data:
print(data)
def __metric_not_found(self, metric_name):
print('Metric named "%s" doesnt exist' % metric_name)
print('Available metrics:')
for metric in self.metrics:
print(metric)
def __model_not_found(self, model_name):
print('Model named "%s" doesnt exist' % model_name)
print('Available models:')
for model in self.models:
print(model)
@name_correct
def get_metric_result(self, metric_name, model_name, data_name=None, *args, **kwargs):
if model_name not in self.models.keys():
self.__model_not_found(model_name)
return
else:
# пока считаем, что все метрики требуют такие обязательные параметры
if data_name is not None:
_, test_features, _, known = self.data[data_name].get_train_test_split()
predicted, _ = self.models[model_name].predict(data=test_features)
if self.data[data_name].attack_label != self.models[model_name].attack_label:
if self.data[data_name].num_classes == 2:
if self.models[model_name].attack_label == -1:
predicted *= -1
else:
known *= -1
else:
print('Model and data have different attack labels. Deal with it yourself.')
else:
if self.models[model_name].attack_label == -1:
predicted *= -1
known *= -1
else:
predicted, known = self.models[model_name].predict()
if metric_name in self.metrics.keys():
return self.metrics[metric_name](known, predicted, *args, **kwargs)
self.__metric_not_found(metric_name)
@name_correct
def add_metric(self, name, metric, replace=False):
if name in self.metrics.keys():
if replace:
print('Metric named "%s" already exist, proceed with replacing' % name)
self.metrics[name] = metric
else:
print('Metric named "%s" already exist, proceed without replacing' % name)
else:
print('Add metric named "%s"' % name)
self.metrics[name] = metric
@name_correct
def add_model(self, model_name, ml_algorithm, data_name, replace=False):
try:
features = self.data[data_name].get_features()
labels = self.data[data_name].get_labels()
except KeyError:
print('Model named "%s" cant be added since provided data_name "%s" doesnt exist'
% (model_name, data_name))
return
if model_name in self.models.keys():
if replace:
print('Model named "%s" already exist, proceed with replacing' % model_name)
self.models[model_name] = Model(model_name, ml_algorithm, features,
labels, self,
attack_label=self.data[data_name].attack_label)
else:
print('Model named "%s" already exist, proceed without replacing' % model_name)
else:
print('Add model named "%s"' % model_name)
self.models[model_name] = Model(model_name, ml_algorithm, features, labels, self)
@name_correct
def add_data(self, name, replace=False, **kwargs):
def really_add_data(data_dict):
dp = DataProcessor(name, **kwargs)
if dp is not None:
data_dict[name] = dp
else:
print('Data named "%s" cant be added due following error:' % name)
print('ERROR: some of features are not in data or df is empty')
if name in self.data.keys():
if replace:
print('Data named "%s" already exist, proceed with replacing' % name)
really_add_data(self.data)
else:
print('Data named "%s" already exist, proceed without replacing' % name)
else:
print('Add data named "%s" ' % name)
really_add_data(self.data)
def __load_gradient_boosting(self):
self.add_model('gb_95-5_notime', GradientBoostingClassifier, 'kyoto2007')
def __load_logistic_regression(self):
self.add_model('lr_95-5_notime', LogisticRegression, 'kyoto2007')
if __name__ == '__main__':
mm = ModelsManager()
print(mm.get_metric_result('average_precision_score', 'gb_95-5_notime'))