-
Notifications
You must be signed in to change notification settings - Fork 20
/
Lab2-preprocess_data.py
81 lines (62 loc) · 2.95 KB
/
Lab2-preprocess_data.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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import argparse
import pickle
parser = argparse.ArgumentParser()
parser.add_argument('--pct_anomalies', default=.01, type=float)
args = parser.parse_args()
pct_anomalies = args.pct_anomalies
data_path = './data/kddcup.data.corrected'
col_names = ["duration","protocol_type","service","flag","src_bytes","dst_bytes","land","wrong_fragment","urgent","hot","num_failed_logins","logged_in",
"num_compromised","root_shell","su_attempted","num_root","num_file_creations","num_shells","num_access_files","num_outbound_cmds",
"is_host_login","is_guest_login","count","srv_count","serror_rate","srv_serror_rate","rerror_rate","srv_rerror_rate","same_srv_rate",
"diff_srv_rate","srv_diff_host_rate","dst_host_count","dst_host_srv_count","dst_host_same_srv_rate","dst_host_diff_srv_rate",
"dst_host_same_src_port_rate","dst_host_srv_diff_host_rate","dst_host_serror_rate","dst_host_srv_serror_rate","dst_host_rerror_rate",
"dst_host_srv_rerror_rate","label"]
df = pd.read_csv(data_path, header=None, names=col_names, index_col=False)
le = LabelEncoder()
le.fit(df.label)
def reduce_anomalies(df, pct_anomalies=.01):
labels = df['label'].copy()
is_anomaly = labels != 'normal.'
num_normal = np.sum(~is_anomaly)
num_anomalies = int(pct_anomalies * num_normal)
all_anomalies = labels[labels != 'normal.']
anomalies_to_keep = np.random.choice(all_anomalies.index, size=num_anomalies, replace=False)
anomalous_data = df.iloc[anomalies_to_keep].copy()
normal_data = df[~is_anomaly].copy()
new_df = pd.concat([normal_data, anomalous_data], axis=0)
return new_df
df = reduce_anomalies(df, pct_anomalies=pct_anomalies)
# capture the categorical variables and one-hot encode them
cat_vars = ['protocol_type', 'service', 'flag', 'land', 'logged_in','is_host_login', 'is_guest_login']
# find unique labels for each category
cat_data = pd.get_dummies(df[cat_vars])
numeric_vars = list(set(df.columns.values.tolist()) - set(cat_vars))
numeric_vars.remove('label')
numeric_data = df[numeric_vars].copy()
numeric_cat_data = pd.concat([numeric_data, cat_data], axis=1)
# capture the labels
labels = df['label'].copy()
# convert labels to integers
integer_labels = le.transform(labels)
# split data into test and train
x_train, x_test, y_train, y_test = train_test_split(numeric_cat_data,
integer_labels,
test_size=.25,
random_state=42)
# save the datasets for later use
preprocessed_data = {
'x_train':x_train,
'y_train':y_train,
'x_test':x_test,
'y_test':y_test,
'le':le
}
# pickle the preprocessed_data
path = 'preprocessed_data_full.pkl'
out = open(path, 'wb')
pickle.dump(preprocessed_data, out)
out.close()