forked from DavidJBianco/Clearcut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clearcut_utils.py
executable file
·77 lines (66 loc) · 2.04 KB
/
clearcut_utils.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
#!/usr/bin/env python
import pandas as pd
import numpy as np
def load_brofile(filename, fields_to_use):
fields = ['ts',
'uid',
'orig_h',
'orig_p',
'resp_h',
'resp_p',
'trans_depth',
'method',
'host',
'uri',
'referrer',
'user_agent',
'request_body_len',
'response_body_len',
'status_code',
'status_msg',
'info_code',
'info_msg',
'filename',
'tags',
'username',
'password',
'proxied orig_fuids',
'orig_mime_types',
'resp_fuids',
'resp_mime_types']
df = pd.read_csv(filename,
header=None,
sep='\t',
names=fields,
skiprows=8,
skipfooter=1,
index_col=False,
quotechar=None,
quoting=3,
engine='python')
return df[fields_to_use]
def create_noise_contrast(df, num_samples):
"""
Create a noise contrasted dataframe from a dataframe. We do this
by sampling columns with replacement one at a time from the original
data, and then stitching those columns together into de-correlated rows.
Parameters
----------
df : dataframe
The enhanced HTTP log dataframe
num_samples : int
Number of new rows to create
Returns
-------
newDf : dataframe
"""
newDf = None
for field in list(df):
#sample the column with replacement.
df1 = df[[field]].sample(n=num_samples, replace=True).reset_index(drop=True)
#add the new column to the answer (or start a new df if this is the first column)
if (newDf is not None):
newDf = pd.concat([newDf, df1], axis = 1)
else:
newDf = df1
return newDf