-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
77 lines (71 loc) · 2.17 KB
/
test.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
from sklearn.ensemble import RandomForestClassifier
import numpy as np
from collections import *
import math
domainlist = []
class Domain:
def __init__(self,_name,_label):
self.name = _name
self.label = _label
self.length = len(_name)
self.number = count(_name)
self.entropy = entropy(_name)
def returnData(self):
return [self.length, self.number, self.entropy]
def returnLabel(self):
if self.label == "notdga":
return 0
else:
return 1
def initData(filename):
with open(filename) as f:
for line in f:
line = line.strip()
if line.startswith("#") or line =="":
continue
tokens = line.split(",")
name = tokens[0]
label = tokens[1]
domainlist.append(Domain(name,label))
def count(domain):
n = 0
for i in domain:
if i.isdigit() == True:
n = n + 1
return n
def entropy(domain):
Map=Counter(domain)
entropy=0.0
for val in Map.values():
entropy-=val/len(domain)*math.log2(val/len(domain))
return entropy
def main():
print("Initialize Raw Objects")
initData("train.txt")
#initData("gooddomaininfo")
featureMatrix = []
labelList = []
print("Initialize Matrix")
for item in domainlist:
featureMatrix.append(item.returnData())
labelList.append(item.returnLabel())
#print(featureMatrix)
print("Begin Training")
clf = RandomForestClassifier(random_state=0)
clf.fit(featureMatrix,labelList)
print("Begin Predicting")
with open("test.txt","r") as r:
with open("result.txt","w") as w:
for line in r:
line = line.strip()
if line.startswith("#") or line =="":
continue
P=clf.predict([[len(line),count(line),entropy(line)]])
#print(P)
if(P==[0]):
w.write(line+",notdga\n")
else:
w.write(line+",dga\n")
print("End")
if __name__ == '__main__':
main()