-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathids_2017_preprocesser.py
70 lines (50 loc) · 1.44 KB
/
ids_2017_preprocesser.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
#import libraries
import os
import pandas as pd
import numpy as np
import random as rn
from scipy.io import arff
#SEED
seed = 23071982
os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(seed)
import sys
def readData(path, delimiter, decimal):
"READ SINGLE FILE"
df=pd.read_csv(path, delimiter=delimiter, decimal=decimal, engine="c", header = 0)
return df
def check_good(value):
return 0.0 if value == "BENIGN" else 1.0
def port_category(value):
if value < 1024 :
return "well_known"
if value < 49152:
return "registered"
return "dynamic"
#MAIN
if __name__ == "__main__":
# --- PARAMETERS ---
# Tuesday , Wednesday, Thursday , Friday
dataset_path = "..."
output_path = "..."
# other parameters
delim = ","
decimal = "."
# --- LOADING DATASET ---
data = readData(dataset_path, delim, decimal)
print("--- LOADED DATASET ---")
# PRINT COLUMN NAMES
print(data.columns)
# PRINT HEAD OF THE DATASET
print(data.head(3))
#PRINT CLASS DISTRIBUTION
print(data["Label"].value_counts())
data["port_type"] = data["Destination_Port"].apply(port_category)
data["class"] = data["Label"].apply(check_good)
data = data.drop("Label", 1)
#PRINT CLASS DISTRIBUTION
print("AFTER")
print(data["class"].value_counts())
print(data["port_type"].value_counts())
# Write
data.to_csv(output_path, sep=delim, decimal=decimal, index=False)