def main(P, src, model): samples = [] # read data for line in src: line = line.strip() if not line[0].isdigit(): continue d = line.split() proto = 1 if d[1] == "TCP" else 2 port = int(d[2]) gt = d[-1] b = int((len(d)-4) / 2) pl_up = [int(x) for x in d[3:3+b]][0:P.i] pl_down = [int(x) for x in d[3+b:-1]][0:P.i] if pl_up[0] == -1 or pl_down[0] == -1: continue v = [proto,port] + pl_up + pl_down samples.append((v, gt)) # load model cls = DT(verb=True) cls.load(model) # test (acc, ratio, err) = cls.score([x[0] for x in samples], [x[1] for x in samples]) print("ok %.3f%%\tin %.3f%%\tof %d K total (%d errors)" % (acc * 100.0, ratio * 100.0, len(samples)/1000.0, err))
def main(P, src, model): samples = [] # read data for line in src: line = line.strip() if not line[0].isdigit(): continue d = line.split() proto = 1 if d[1] == "TCP" else 2 port = int(d[2]) gt = d[-1] stats = [int(x) for x in d[3:-1]] if stats[4] == 0 or stats[12] == 0: continue v = [proto,port] + stats samples.append((v, gt)) # load model cls = DT() cls.load(model) cls.algo.set_params(n_jobs=-1) # test (acc, ratio, err) = cls.score([x[0] for x in samples], [x[1] for x in samples]) print("ok %.3f%%\tin %.3f%%\tof %d K total (%d errors)" % (acc * 100.0, ratio * 100.0, len(samples)/1000.0, err))
def main(P, src, model): samples = [] # read data for line in src: line = line.strip() if not line[0].isdigit(): continue d = line.split() proto = 1 if d[1] == "TCP" else 2 port = int(d[2]) gt = d[-1] b = int((len(d)-4) / 2) szup = [int(x) for x in d[3:3+b]][0:P.i] szdown = [int(x) for x in d[3+b:-1]][0:P.i] if szup[0] == 0 or szdown[0] == 0: continue v = [proto,port] + szup + szdown samples.append((v, gt)) # load model #cls = kNN(k=P.k, verb=True) #cls = kNN(k=P.k) cls = DT() cls.load(model) cls.algo.set_params(n_jobs=-1) # test (acc, ratio, err) = cls.score([x[0] for x in samples], [x[1] for x in samples]) print("ok %.3f%%\tin %.3f%%\tof %d K total (%d errors)" % (acc * 100.0, ratio * 100.0, len(samples)/1000.0, err))
def main(P, src, dst): samples = [] total = 0 # read data for line in src: line = line.strip() if not line[0].isdigit(): continue total += 1 d = line.split() proto = 1 if d[1] == "TCP" else 2 port = int(d[2]) gt = d[-1] b = int((len(d)-4) / 2) pl_up = [int(x) for x in d[3:3+b]][0:P.i] pl_down = [int(x) for x in d[3+b:-1]][0:P.i] if pl_up[0] == -1 or pl_down[0] == -1: continue v = [proto,port] + pl_up + pl_down samples.append((v, gt)) print("read %d samples out of %d total (%.2f%%)" % (len(samples), total, 100.0*len(samples)/total)) # take random samples if P.t > 0: samples = random.sample(samples, P.t+P.T) train = samples[:P.t] test = samples[P.t:] else: train = samples test = [] # train knc = DT() knc.fit([x[0] for x in train], [x[1] for x in train]) # test if len(test) > 0: (acc, ratio, err) = knc.score([x[0] for x in test], [x[1] for x in test]) print("ok %.3f%%\tin %.3f%%\tof %d K total (%d errors)" % (acc * 100.0, ratio * 100.0, len(test)/1000.0, err)) # store model if dst: knc.store(dst)