-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBenignVSRestScript.py
50 lines (36 loc) · 1.27 KB
/
BenignVSRestScript.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
import pandas as pd
import numpy as np
# This script was created in order to obtain stratified samples of 5.000 samples
# for training and testing from the whole Wednesday dataset of about 690.000 samples
# In this dataset we are going to label BENIGNs as 1s and all the attacks as 0s
dataset = pd.read_csv("data.csv")
# Obtaining training data
N = 5000 # number of samples
# stratified set of N samples
subset = dataset.groupby(' Label', group_keys=False).apply(lambda x: x.sample(int(np.rint(N*len(x)/len(dataset))))).sample(frac=1).reset_index(drop=True)
# labels
y = []
for i in subset[' Label']:
if i == 'BENIGN':
y.append(1)
else:
y.append(0)
# We create a new frame to later save to a .csv file
data = subset[subset.columns[0:-1]]
data['Label'] = y
data.to_csv('train.csv')
# Obtaining testing data
N = 5000 # number of samples
# stratified set of N samples
subset = dataset.groupby(' Label', group_keys=False).apply(lambda x: x.sample(int(np.rint(N*len(x)/len(dataset))))).sample(frac=1).reset_index(drop=True)
# labels
y = []
for i in subset[' Label']:
if i == 'BENIGN':
y.append(1)
else:
y.append(0)
# We create a new frame to later save to a .csv file
data = subset[subset.columns[0:-1]]
data['Label'] = y
data.to_csv('test.csv')