-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxMin_Normalizer.py
More file actions
206 lines (153 loc) · 4.75 KB
/
maxMin_Normalizer.py
File metadata and controls
206 lines (153 loc) · 4.75 KB
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
import numpy as np
import scipy.stats as st
import math,time
import sys
from sklearn.metrics import mean_squared_error
class maxMin_Normalizer:
#output=str(sys.argv[1])+'max-min'
#output_file=open(output,'w')
def run(self):
beg=time.time()
#output=str(sys.argv[1])+'max-min'
def dataPrepare(item):
''' get the values, remove the categorical data'''
a=item.split(',')
label=a[len(a)-1].split('\n')[0]
data=a[5:len(a)-1]#removing IPsrc,IPdst,portsrc,portdsc,proto
return data
def getValues(janela):
''' take the local values of the current batch'''
vmax=[]
vmin=[]
umean=[]
sigmin=[]
for i in range(len(janela[0])):
column=janela[:,i].astype(np.float64)
vmax.append(max(column))
vmin.append(min(column))
umean.append(np.mean(column))
sigmin.append(np.std(column))
return vmax,vmin,umean,sigmin
def normalizing(janela,refMax,refMin):
# normalized = (x-min(x))/(max(x)-min(x))
global N
for i in range(N):
aux=np.subtract(janela[:,i].astype(np.float64),refMin[i])
aux2=np.subtract(refMax[i],refMin[i])
if (aux2 == 0):
janela[:,i]=0.5 #https://docs.tibco.com/pub/spotfire/7.0.0/doc/html/norm/norm_scale_between_0_and_1.htm
#If Emax is equal to Emin then Normalized (ei) is set to 0.5.
else:
janela[:,i]=np.nan_to_num(np.divide(aux,aux2).tolist())
for j in range(len(janela[:,i])):
if float(janela[:,i][j])>1:
janela[:,i][j]=1
return janela
def verifyMetrics(localMax,localMin,refMax,refMin):
'''
function to verify if the values of the current chunks are different that references. (procedure metrics in paper)
'''
global N
global windowSize
global m1 #metric1 treshold
global m2 #metric2 threshold
metric1 = False
metric2 = False
metric1Counter = 0
for i in range(N):
if (localMin[i] < refMin[i]):
metric1Counter+=1
if (localMax[i] > refMax[i]):
metric1Counter+=1
if refMin[i] == 0: #to avoid zero division
if ((refMin[i]-localMin[i])/1 > m2):
metric2 = True
else:
if ((refMin[i]-localMin[i])/refMin[i] > m2):
metric2 = True
if refMax[i]==0:#to avoid zero division
if ((localMax[i]-refMax[i])/1 > m2):
metric2 = True
else:
if ((localMax[i]-refMax[i])/refMax[i] > m2):
metric2 = True
if (metric1Counter/windowSize > m1):
metric1 = True
return metric1,metric2
global windowSize
#windowSize=int(sys.argv[1]) #as paper
windowSize=500
global N
N=40 #number of features
global m1 #metric1 treshold
global m2 #metric2 threshold
m1=0.05
m2=0.05
numberBins=math.ceil(math.sqrt(N))
global windowsNumber
windowsNumber = 0
janMax = [] #janela de valores medios. Vou manter N valores
janMin = []
janMean = []
janStd = []
histogram = {} #histogram with frequency of the samples
for j in range(N):
histogram[j]=0
files=open('classes-17-end.out','r')
saida = open("max-min-classes-17-norm.out", "w")
lines=files.readlines()
np.set_printoptions(precision=3)
np.set_printoptions(suppress=True,formatter={'float_kind':'{:f}'.format})
batch=[]
#a=lines[0:100000]
for i in lines:
batch.append(dataPrepare(i))
before=batch
batch=np.array(batch)
print 'file loaded'
jan=[] #take a windows everytime we have a batch
beg=time.time()
for i in range(0,len(batch), windowSize): #
jan = batch[i:i+windowSize]
#calGlobal(localMax,localMin,localMean,localStd)
if windowsNumber == 0:
refMax,refMin,localMean,localStd = getValues(jan)
salida=normalizing(jan,refMax,refMin)
else:
localMax,localMin,localMean,localStd=getValues(jan)
metric1,metric2=verifyMetrics(localMax,localMin,refMax,refMin)
if (metric1 and metric2):
refMax=localMax
refMin=localMin
t=normalizing(jan,refMax,refMin)
if windowsNumber!=0:
salida=np.vstack((salida,t))
windowsNumber+=1 #incrementing this number
if (windowsNumber % 1000) == 0:
print "windowsNumber: "+str(windowsNumber)
# lower, upper = 0, 1
# salida=np.asfarray(salida)
# salidaNew = [lower + (upper - lower) * x for x in salida]
end=time.time()-beg
''' to write in file'''
for k in salida:
tmp = []
for l in k:
tmp.append(str(l))
linhaSaida = ",".join(tmp)
saida.write(linhaSaida+"\n")
end=time.time()-beg
saida.write(str('processing time : '+str(end)))
saida.close()
return salida,end
'''
to calculate the mean square error
'''
# original_maxmin=[]
# t=np.asfarray(before)
# salida=np.asfarray(salida)
# for i in range(N):
# original_maxmin.append(mean_squared_error(t[:,i],salida[:,i]))
# output_file.write(str(original_maxmin)+'\n')
# output_file.write(str('processing time : '+str(end)))
# output_file.close()