-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketized.py
463 lines (386 loc) · 18.5 KB
/
Packetized.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# -*- coding: utf-8 -*-
"""
@title: Packetized Machine Learning in Support Vector Machines
@author: Ignacio Melero Miguel
"""
# Necessary imports.
# Alternative for timeit: import time and use time.clock() or time.time() for Python >= 3.3
# Tested with Python 2.7.11
from PacketizedPackage import PacketizedModule as pck
from timeit import default_timer as timer
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
plt.rcParams['mathtext.fontset'] = 'stix'
plt.rcParams['font.family'] = 'STIXGeneral'
####
# Comparison of training and predicting times and AUC with a SVM classifier using
# the Packetized technique regarding to the number of packets.
####
scenario = 'a'
samples = 100000
plt_packets = [2, 4, 8, 16, 32, 64, 128, 256]
plt_total_times_training_packetized = []
plt_total_times_predicting_packetized = []
plt_total_auc_packetized = []
plt_total_score_packetized = []
# Load an existing dataset.
#X = []
#Y = []
#for line in open('magic.dat', 'r'):
# item = np.asarray(line.strip().split(","))
# X.append(item[:-1])
# Y.append((item[-1]))
#X = np.array(X, 'float')
#Y = np.array(Y)
#X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.2)
# Generate a synthetic dataset.
# Scenario A
if scenario is 'a':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 100, 0, 0, 2, 1,
1, 0, test_size=.2)
# Scenario B
if scenario is 'b':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 4, 0, 0, 2,
2, 1, 0.1, test_size=.2)
# Scenario C
if scenario is 'c':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 4, 0, 0, 4,
2, 1, 0.1, test_size=.2)
for packets in plt_packets:
tic_training_packetized = timer()
models = pck.train_packetized(X_train, Y_train, packets=packets, False, 'rbf')
toc_training_packetized = timer()
total_time_training_packetized = toc_training_packetized - tic_training_packetized
plt_total_times_training_packetized.append(total_time_training_packetized)
tic_predicting_packetized = timer()
Y_pred = pck.predict_packetized(models, X_test)
toc_predicting_packetized = timer()
total_time_predicting_packetized = toc_predicting_packetized - tic_predicting_packetized
plt_total_times_predicting_packetized.append(total_time_predicting_packetized)
if scenario is 'c':
plt_total_score_packetized.append(accuracy_score(Y_test, Y_pred)*100)
else:
plt_total_auc_packetized.append(roc_auc_score(Y_test, Y_pred)*100)
plt.figure()
plt.xlabel('Number of packets', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Total time of training with packetized ('+str(samples)+' samples)', weight='bold')
plt.plot(plt_packets, plt_total_times_training_packetized, marker='o', label="Time training with packetized")
plt.xticks(plt_packets)
# plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.legend(['Time of training'])
plt.figure()
plt.xlabel('Number of packets', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Total time of predicting with packetized ('+str(samples)+' samples)', weight='bold')
plt.plot(plt_packets, plt_total_times_predicting_packetized, marker='o', label="Time predicting with packetized")
plt.xticks(plt_packets)
#plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.legend(['Time of predicting'])
if scenario is 'c':
plt.figure()
plt.xlabel('Number of packets', weight='bold')
plt.ylabel('%', weight='bold')
plt.title(r'Accuracy score', weight='bold')
plt.plot(plt_packets, plt_total_score_packetized, marker='o', label="Accuracy score")
# plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xticks(plt_packets)
axes = plt.gca()
# axes.set_xlim([xmin,xmax])
axes.set_ylim([50, 100])
plt.legend(['Accuracy score'])
plt.show()
else:
plt.figure()
plt.xlabel('Number of packets', weight='bold')
plt.ylabel('%', weight='bold')
plt.title(r'AUC ROC', weight='bold')
plt.plot(plt_packets, plt_total_auc_packetized, marker='o', label="AUC ROC")
# plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xticks(plt_packets)
axes = plt.gca()
# axes.set_xlim([xmin,xmax])
axes.set_ylim([50,100])
plt.legend(['AUC'])
plt.show()
####
# Comparison of training and predicting times and AUC with a SVM classifier using and
# without using the Packetized technique.
####
scenario = 'a'
packets = 64
plt_samples = [10e+2, 10e+3, 10e+4, 10e+5]
plt_total_times_training_normal = []
plt_total_times_training_packetized = []
for samples in plt_samples:
# Scenario A
if scenario is 'a':
X_train, Y_train = pck.create_only_training_dataset(samples, 100, 100, 0, 0, 2, 1, 1, 0)
# Scenario B
if scenario is 'b':
X_train, Y_train = pck.create_only_training_dataset(samples, 100, 4, 0, 0, 2, 2, 1, 0.1)
# Scenario C
if scenario is 'c':
X_train, Y_train = pck.create_only_training_dataset(samples, 100, 4, 0, 0, 4, 2, 1, 0.1)
tic_training_normal = timer()
clf = pck.train(X_train, Y_train, False, 'rbf')
toc_training_normal= timer()
total_time_training_normal = toc_training_normal - tic_training_normal
plt_total_times_training_normal.append(total_time_training_normal)
tic_training_packetized = timer()
clf_packetized = pck.train_packetized(X_train, Y_train, packets, False, 'rbf')
toc_training_packetized = timer()
total_time_training_packetized = toc_training_packetized - tic_training_packetized
plt_total_times_training_packetized.append(total_time_training_packetized)
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Training time comparison', weight='bold')
plt.plot(plt_samples, plt_total_times_training_packetized, marker='s', label="Time training with packetized")
plt.plot(plt_samples, plt_total_times_training_normal, marker='o', label="Time training normal")
plt.xticks(plt_samples)
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.legend(['Training time '+str(packets)+' packets', 'Training time 1 packet'])
plt_total_times_training_normal = []
plt_total_times_training_packetized = []
plt_total_times_predicting_normal = []
plt_total_times_predicting_packetized = []
plt_total_auc_normal = []
plt_total_auc_packetized = []
plt_total_score_normal = []
plt_total_score_packetized = []
for samples in plt_samples:
# Scenario A
if scenario is 'a':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 100, 0, 0, 2, 1,
1, flip_y=0, test_size=.2)
# Scenario B
if scenario is 'b':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 4, 0, 0, 2,
2, 1, 0.1, test_size=.2)
# Scenario C
if scenario is 'c':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 4, 0, 0, 4,
2, 1, 0.1, test_size=.2)
tic_training_normal = timer()
clf = pck.train(X_train, Y_train, False, 'rbf')
toc_training_normal = timer()
total_time_training_normal = toc_training_normal - tic_training_normal
plt_total_times_training_normal.append(total_time_training_normal)
tic_training_packetized = timer()
models = pck.train_packetized(X_train, Y_train, packets, False, 'rbf')
toc_training_packetized = timer()
total_time_training_packetized = toc_training_packetized - tic_training_packetized
plt_total_times_training_packetized.append(total_time_training_packetized)
tic_predicting_normal = timer()
Y_pred = pck.predict(clf, X_test)
toc_predicting_normal = timer()
total_time_predicting_normal = toc_predicting_normal - tic_predicting_normal
plt_total_times_predicting_normal.append(total_time_predicting_normal)
tic_predicting_packetized = timer()
Y_pred_packetized = pck.predict_packetized(models, X_test)
toc_predicting_packetized = timer()
total_time_predicting_packetized = toc_predicting_packetized - tic_predicting_packetized
plt_total_times_predicting_packetized.append(total_time_predicting_packetized)
if scenario is 'c':
plt_total_score_normal.append(accuracy_score(Y_test, Y_pred)*100)
plt_total_score_packetized.append(accuracy_score(Y_test, Y_pred_packetized)*100)
else:
plt_total_auc_normal.append(roc_auc_score(Y_test, Y_pred)*100)
plt_total_auc_packetized.append(roc_auc_score(Y_test, Y_pred_packetized)*100)
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Predicting time comparison', weight='bold')
plt.plot(plt_samples, plt_total_times_predicting_packetized, marker='s', label="Time predicting with packetized")
plt.plot(plt_samples, plt_total_times_predicting_normal, marker='o', label="Time predicting normal")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.xticks(plt_samples)
plt.legend(['Predicting time '+str(packets)+' packets', 'Predicting time 1 packet'])
if scenario is 'c':
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('%', weight='bold')
plt.title(r'Accuracy score comparison', weight='bold')
plt.plot(plt_samples, plt_total_score_packetized, marker='s', label="Accuracy score packetized")
plt.plot(plt_samples, plt_total_score_normal, marker='o', label="Accuracy score normal")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
plt.xticks(plt_samples)
axes = plt.gca()
# axes.set_xlim([xmin,xmax])
axes.set_ylim([50, 100])
plt.legend(['Accuracy score '+str(packets)+' packets', 'Accuracy score 1 packet'])
plt.show()
else:
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('%', weight='bold')
plt.title(r'AUC ROC comparison', weight='bold')
plt.plot(plt_samples, plt_total_auc_packetized, marker='s', label="AUC ROC with packetized")
plt.plot(plt_samples, plt_total_auc_normal, marker='o', label="AUC ROC normal")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.xticks(plt_samples)
axes = plt.gca()
# axes.set_xlim([xmin,xmax])
axes.set_ylim([50, 100])
plt.legend(['AUC ROC '+str(packets)+' packets','AUC ROC 1 packet'])
plt.show()
####
# Comparison of training times with a SVM classifier using
# the Packetized technique regarding to the number of features of the dataset.
####
samples = 10000
packets = 64
plt_features = [1, 10e+0, 10e+1, 10e+2]
plt_total_times_training_normal = []
plt_total_times_training_packetized = []
for features in plt_features:
X_train, Y_train = pck.create_only_training_dataset(samples, features, features, 0, 0, 2, 1, 1, 0)
tic_training_normal = timer()
clf = pck.train(X_train, Y_train, False, 'rbf')
toc_training_normal= timer()
total_time_training_normal = toc_training_normal - tic_training_normal
plt_total_times_training_normal.append(total_time_training_normal)
tic_training_packetized = timer()
clf_packetized = pck.train_packetized(X_train, Y_train, packets, False, 'rbf')
toc_training_packetized = timer()
total_time_training_packetized = toc_training_packetized - tic_training_packetized
plt_total_times_training_packetized.append(total_time_training_packetized)
plt.figure()
plt.xlabel('Features', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Training time comparison', weight='bold')
plt.plot(plt_features, plt_total_times_training_packetized, marker='s', label="Time training with packetized")
plt.plot(plt_features, plt_total_times_training_normal, marker='o', label="Time training normal")
plt.xticks(plt_features)
plt.legend(['Training time '+str(packets)+' packets','Training time 1 packet'])
plt.show()
#####
# Comparison of training and predicting times and AUC with a SVM classifier using
# the Packetized technique and Bagging.
#####
scenario = 'a'
packets = 64
plt_samples = [10e+2, 10e+3, 10e+4, 10e+5]
plt_total_times_training_packetized = []
plt_total_times_training_bagging = []
for samples in plt_samples:
# Scenario A
if scenario is 'a':
X_train, Y_train = pck.create_only_training_dataset(samples, 100, 100, 0, 0, 2, 1, 1, 0)
# Scenario B
if scenario is 'b':
X_train, Y_train = pck.create_only_training_dataset(samples, 100, 4, 0, 0, 2, 2, 1, 0.1)
# Scenario C
if scenario is 'c':
X_train, Y_train = pck.create_only_training_dataset(samples, 100, 4, 0, 0, 4, 2, 1, 0.1)
tic_training_packetized = timer()
clf_packetized = pck.train_packetized(X_train, Y_train, packets, False, 'rbf')
toc_training_packetized = timer()
total_time_training_packetized = toc_training_packetized - tic_training_packetized
plt_total_times_training_packetized.append(total_time_training_packetized)
tic_training_bagging = timer()
clf_bagging = pck.train_bagging(X_train, Y_train)
toc_training_bagging = timer()
total_time_training_bagging = toc_training_bagging - tic_training_bagging
plt_total_times_training_bagging.append(total_time_training_bagging)
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Training time comparison', weight='bold')
plt.plot(plt_samples, plt_total_times_training_packetized, marker='o', label="Time training with packetized")
plt.plot(plt_samples, plt_total_times_training_bagging, marker='s', label="Time training with bagging")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.xticks(plt_samples)
plt.legend(['Training time using Packetized ('+str(packets)+' packets)', 'Training time using Bagging'])
plt.show()
plt_total_times_training_bagging = []
plt_total_times_training_packetized = []
plt_total_times_predicting_bagging = []
plt_total_times_predicting_packetized = []
plt_total_auc_bagging = []
plt_total_auc_packetized = []
plt_total_score_bagging = []
plt_total_score_packetized = []
for samples in plt_samples:
# Scenario A
if scenario is 'a':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 100, 0, 0, 2, 1,
1, 0, test_size=.2)
# Scenario B
if scenario is 'b':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 4, 0, 0, 2,
2, 1, 0.1, test_size=.2)
# Scenario C
if scenario is 'c':
X_train, X_test, Y_train, Y_test = pck.create_training_testing_dataset(samples, 100, 4, 0, 0, 4,
2, 1, 0.1, test_size=.2)
tic_training_packetized = timer()
models = pck.train_packetized(X_train, Y_train, packets, False, 'rbf')
toc_training_packetized = timer()
total_time_training_packetized = toc_training_packetized - tic_training_packetized
plt_total_times_training_packetized.append(total_time_training_packetized)
tic_training_bagging = timer()
clf_bagging = pck.train_bagging(X_train, Y_train)
toc_training_bagging = timer()
total_time_training_bagging = toc_training_bagging - tic_training_bagging
plt_total_times_training_bagging.append(total_time_training_bagging)
tic_predicting_packetized = timer()
Y_pred_packetized = pck.predict_packetized(models, X_test)
toc_predicting_packetized = timer()
total_time_predicting_packetized = toc_predicting_packetized - tic_predicting_packetized
plt_total_times_predicting_packetized.append(total_time_predicting_packetized)
tic_predicting_bagging = timer()
Y_pred_bagging = pck.predict(clf_bagging, X_test)
toc_predicting_bagging = timer()
total_time_predicting_bagging = toc_predicting_bagging - tic_predicting_bagging
plt_total_times_predicting_bagging.append(total_time_predicting_bagging)
if scenario is 'c':
plt_total_score_packetized.append(accuracy_score(Y_test, Y_pred_packetized)*100)
plt_total_score_bagging.append(accuracy_score(Y_test, Y_pred_bagging)*100)
else:
plt_total_auc_packetized.append(roc_auc_score(Y_test, Y_pred_packetized)*100)
plt_total_auc_bagging.append(roc_auc_score(Y_test, Y_pred_bagging)*100)
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('Time in seconds', weight='bold')
plt.title(r'Predicting time comparison', weight='bold')
plt.plot(plt_samples, plt_total_times_predicting_packetized, marker='o', label="Time predicting packetized")
plt.plot(plt_samples, plt_total_times_predicting_bagging, marker='s', label="Time predicting with bagging")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.xticks(plt_samples)
plt.legend(['Predicting time using Packetized ('+str(packets)+' packets)', 'Predicting time using Bagging'])
plt.show()
if scenario is 'c':
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('%', weight='bold')
plt.title(r'Accuracy score comparison', weight='bold')
plt.plot(plt_samples, plt_total_score_packetized, marker='o', label="Accuracy score packetized")
plt.plot(plt_samples, plt_total_score_bagging, marker='s', label="Accuracy score bagging")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
plt.xticks(plt_samples)
axes = plt.gca()
# axes.set_xlim([xmin,xmax])
axes.set_ylim([50, 100])
plt.legend(['Accuracy score using Packetized ('+str(packets)+' packets)', 'Accuracy score using Bagging'])
plt.show()
else:
plt.figure()
plt.xlabel('Samples', weight='bold')
plt.ylabel('%', weight='bold')
plt.title(r'AUC ROC comparison', weight='bold')
plt.plot(plt_samples, plt_total_auc_packetized, marker='o', label="AUC ROC packetized")
plt.plot(plt_samples, plt_total_auc_bagging, marker='s', label="AUC ROC with bagging")
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.xticks(plt_samples)
axes = plt.gca()
# axes.set_xlim([xmin,xmax])
axes.set_ylim([50, 100])
plt.legend(['AUC ROC using Packetized ('+str(packets)+' packets)', 'AUC ROC using Bagging'])
plt.show()
print 'END'