-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_dt.py
More file actions
executable file
·182 lines (162 loc) · 5.12 KB
/
start_dt.py
File metadata and controls
executable file
·182 lines (162 loc) · 5.12 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
import csv
import numpy as np
class Tree(object):
def set_leaf(self):
self.is_leaf = True
def check_leaf(self):
return self.is_leaf
def set_class(self, v):
self.c = v
def set_node_value(self, v):
self.value = v
def set_instances(self, g):
self.instaces = g
def __init__(self, h):
self.dict = {}
self.height = h
self.is_leaf = False
self.value = 0
self.c = 0
class DecisionTree():
def predict(self, testfile):
f = open(testfile, 'rb')
reader = csv.reader((line.replace(', ', ',') for line in f), delimiter=',')
X = list(reader)
p = self.np.array(X)
for i in p:
self.check_trav(self.t, i)
print "Accuracy: ", (self.cor * 100.0) / (self.wor + self.cor)
def check_trav(self, t, X):
if t.check_leaf() == True:
if t.c == int(X[14]):
self.cor = self.cor + 1
else:
self.wor = self.wor + 1
self.l.append(t.c)
else:
value = X[t.value]
if value in t.dict:
self.check_trav(t.dict[value], X)
else:
if t.c == int(X[14]):
self.cor = self.cor + 1
else:
self.wor = self.wor + 1
self.l.append(t.c)
def train(self, trainfile):
f = open(trainfile, 'rb')
reader = csv.reader((line.replace(', ', ',') for line in f), delimiter=',')
X = list(reader)
r, c = np.shape(X)
X = np.array(X)
self.t = self.generate_dt(X, 0, [1, 3, 5, 6, 7, 8, 9, 13])
def get_groups(self, d, attr):
dict = {}
x = set(d[:, attr[0]])
for i in x:
dict[i] = []
for i in d:
dict[i[attr[0]]].append(np.array(i))
return dict
def get_groups_for_two(self,d,attr):
dict=self.get_groups(d,[14])
dict2={}
x=set(d[:,attr[0]])
for i in dict.keys():
for j in x:
dict2[j+" "+i]=[]
for i in dict.keys():
for j in dict[i]:
p=j[attr[0]]+" "+i
dict2[p].append(j)
return dict2
def generate_dt(self, X, h, attributes):
t = Tree(h)
g = self.get_groups(X, [14])
if len(attributes) > 2 and len(g.keys()) > 1 :
if len(g['0']) > len(g['1']):
t.set_class(0)
else:
t.set_class(1)
# print len(g.groups)
if len(g.keys()) <= 1:
l = g.keys()[0]
t.set_class(int(l))
t.set_leaf()
# print "leaf"
elif len(attributes) <= 2:
if len(g['0']) > len(g['1']):
t.set_leaf()
t.set_class(0)
# print "leaf 0"
else:
t.set_leaf()
t.set_class(1)
# print "leaf 1"
else:
min = self.find_min_entropy(X, attributes)
g = self.get_groups(X, [attributes[min]])
t.set_node_value(attributes[min])
# attributes.remove(attributes[min])
pp = []
for i in attributes:
if i is not attributes[min]:
pp.append(i)
for i in g.keys():
df = g[i]
df=np.array(df)
p = self.generate_dt(df, h + 1, pp)
t.dict[str(i)] = p
return t
def find_min_entropy(self, X, attributes):
min_ent = []
for i in attributes:
min_ent.append(self.calculate_entropy(X, i))
index, value = max(enumerate(min_ent), key=self.op.itemgetter(1))
return index
def calculate_entropy(self, X, i):
l=X[0][i]
if type(X[0][i]) in [self.np.int64, self.np.int64]:
return -9999
pass
else:
g = self.get_groups(X, [i])
entropy = 0
for gi in g.keys():
noofcat = len(g[gi])
sg = np.array(g[gi])
subg = self.get_groups_for_two(sg,[i])
PB = noofcat / float(10000)
class_ent = 0
for subgi in subg.keys():
noofcatp = len(subg[subgi])
prob = noofcatp / float(noofcat)
class_ent = class_ent + prob * self.mt.log(prob, 2)
PB = PB * class_ent
entropy = entropy + PB
# print i,entropy
return entropy
pass
def print_tree(self):
self.pretty(self.t)
def pretty(self,t, indent=0):
if t.check_leaf()==True:
print '\t' * indent*2,t.c
else:
for i in t.dict:
print '\t' * indent*2,t.value,i
self.pretty(t.dict[i],indent+1)
def __init__(self):
import numpy
import math
import operator
self.op=operator
self.np = numpy
self.mt= math
self.l = []
self.cor = 0
self.wor = 0
model = DecisionTree()
model.train("train1.csv")
model.print_tree()
model.predict("test.csv")