/
dtree.py
372 lines (338 loc) · 10.8 KB
/
dtree.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
from __future__ import division
from lib import *
from demos import *
from table import *
from fi import *
from Abcd import *
# from learn import *
from dtree import *
# from start import *
import sys
sys.dont_write_bytecode = True
def writefile(s):
global The
f = open(The.option.resultname, 'a')
f.write(s+'\n')
f.close()
def rankedFeatures(rows,t,features=None):
features = features if features else t.indep
klass = t.klass[0].col # why only for the first dependable
def ranked(f):
syms, at, n = {}, {}, len(rows)
for x in f.counts.keys():
syms[x] = Sym() #example: {(0, 6): <counts.Sym object at 0x106f93fd0>,
#(7, 17): <counts.Sym object at 0x106f6d610>}
for row in rows:
key = row.cells[f.col]
val = row.cells[klass]
syms[key] + val # val is class
at[key] = at.get(key,[]) + [row] #at[(0,6)][0].cells
#[(0, 6), (100, 123), (0, 122), (0, 99), (14, 120), (26.4, 67.1),
#(0.528, 2.42), (21, 28), 'testednegative']
e = 0
for val in syms.values():
if val.n:
e += val.n/n * val.ent() # calculate ent of the new feature
return e,f,syms,at
return sorted(ranked(f) for f in features) # return the features sorted by the ent for pruning
def infogain(t,opt=The.tree): # feature subset selection based on infogain, pruning.
def norm(x): return (x - lo)/(hi - lo+0.0001)
for f in t.headers:
f.selected=False # what does selected mean?
lst = rankedFeatures(t._rows,t) # e, f, syms, at (ranked according to e)
n = int(len(lst)*opt.infoPrune) # prune the tree to be a smaller one
n = max(n,1)
for _,f,_,_ in lst[:n]:
f.selected=True
out = [f for e,f,syms,at in lst[:n]]
# e = [e for e,f,syms,at in lst[:n]]
# print e
# for i in out:
# print i.name,
return out
def tdiv1(t,rows,lvl=-1,asIs=10**32,up=None,features=None,branch=[],
f=None,val=None,opt=None):
here = Thing(t=t,kids=[],f=f,val=val,up=up,lvl=lvl,rows=rows,modes={},branch=branch)
if f and opt.debug:
print ('|.. ' * lvl) + f.name ,"=",val,len(rows)
here.mode = classStats(here).mode() # get corresponding dependable value with larger nubmer(most)
if lvl > 10 : return here
if asIs==0: return here
_, splitter, syms,splits = rankedFeatures(rows,t,features)[0]
#splits: dict, keys are splitters, values are corresponding rows
#syms : dic, keys are cluster ID, clusters in this feature(coloumn)
#splitter: one feature, with the least entropy
#_:entropy for this feature
for key in sorted(splits.keys()):
someRows = splits[key]
toBe = syms[key].ent()
if opt.variancePrune and lvl > 1 and toBe >= asIs: # why have this?
continue
if opt.min <= len(someRows) < len(rows) :
here.kids += [tdiv1(t,someRows,lvl=lvl+1,asIs=toBe,features=features,
up=here,f=splitter,val=key,branch=branch + [(splitter,key)],opt=opt)]
return here
def tdiv(tbl,rows=None,opt=The.tree):
rows = rows or tbl._rows # each cell in the row is a range value based on entropy dividing scheme.
features= infogain(tbl,opt) # for diabetes, after pruning, features: plas and mass;
# opt.min = len(rows)**The.option.treeMin
tree = tdiv1(tbl,rows,opt=opt,features=features,branch=[]) #
if opt.prune:
modes(tree)
prune(tree)
global The
if not The.option.tuning:
readfeatures(tree)
# pdb.set_trace()
writefile(str(The.option.wherefeatures))
The.option.wherefeatures = []
return tree
def readfeatures(n):
if n.branch !=[]:
if n.branch[-1][0].name not in The.option.wherefeatures:
The.option.wherefeatures.append(n.branch[-1][0].name)
for kid in n.kids:
readfeatures(kid)
def modes(n): # what's the function?
if not n.modes:
n.modes = {n.mode: True}
for kid in n.kids:
for mode in modes(kid):
n.modes[mode]=True
return n.modes
def nmodes(n): return len(n.modes.keys())
def prune(n):
if nmodes(n)==1: n.kids=[]
for kid in n.kids:
prune(kid)
def classStats(n):
klass=lambda x: x.cells[n.t.klass[0].col]
sss=Sym(klass(x) for x in n.rows)
return sss
def showTdiv(n,lvl=-1):
if n.f:
say( ('|..' * lvl) + str(n.f.name)+ "="+str(n.val) + \
"\t:" + str(n.mode) + " #" + str(nmodes(n)))
if n.kids:
nl();
for k in n.kids:
showTdiv(k,lvl+1)
else:
s=classStats(n)
print ' '+str(int(100*s.counts[s.mode()]/len(n.rows)))+'% * '+str(len(n.rows))
def dtnodes(tree):
if tree:
yield tree
for kid in tree.kids:
for sub in dtnodes(kid):
yield sub
def dtleaves(tree):
for node in dtnodes(tree):
#print "K>", tree.kids[0].__dict__.keys()
if not node.kids:
yield node
#if tree:
# if tree.kids:
# for kid in tree.kids:
# for leaf in leaves(kid):
# yield leaf
#else:
# yield tree
def xval(tbl,m=None,n=None,opt=The.tree):
m = m or The.tree.m
n = n or The.tree.n
cells = map(lambda row: opt.cells(row), tbl._rows)
all = m*n
for i in range(m):
print "*" * all
cells = shuffle(cells)
div = len(cells)//n
for j in range(n):
all -= 1
lo = j*div
hi = lo + div
train = clone(tbl,cells[:lo]+cells[hi:])
test = map(Row,cells[lo:hi])
yield test,train
def apex(test,tree,opt=The.tree):
"""apex= leaf at end of biggest (most supported)
branch that is selected by test in a tree"""
def equals(val,span):
if val == opt.missing or val==span:
return True
else:
if isinstance(span,tuple):
lo,hi = span
return lo <= val <= hi
else:
return span == val
def apex1(cells,tree):
found = False
for kid in tree.kids:
val = cells[kid.f.col]
if equals(val,kid.val):
for leaf in apex1(cells,kid):
found = True
yield leaf
if not found:
yield tree
leaves= [(len(leaf.rows),leaf)
for leaf in apex1(opt.cells(test),tree)]
# a = leaves
# b = sorted(a)
# c =last(b)
return second(last(sorted(leaves)))
def classify(test,tree,opt=The.tree):
return apex(test,tree,opt=The.tree).mode
def improve(test,tree,opt=The.tree) :
return change(test,tree,opt.better,opt)
def degrade(test,tree,opt=The.tree) :
return change(test,tree,opt.worse,opt)
def change(test,tree,how,opt=The.tree):
leaf1 = apex(test,tree,opt)
new = old = leaf.mode
if how(leaf):
copy = opt.cells(test)[:]
for col,val in how(leaf1).items():
copy[col] = val
new = classify(Row(copy),tree,opt)
return old,new
def jumpUp( test,tree,opt=The.tree):
return jump(test,tree,opt.better,opt)
def jumpDown(test,tree,opt=The.tree):
return jump(test,tree,opt.worse,opt)
def jump(test,tree,how,opt=The.tree):
toBe = asIs = apex(test,tree,opt)
if how(asIs):
copy = opt.cells(test)[:]
for col,val in how(asIs).items():
copy[col] = val
toBe = apex(Row(copy),tree,opt)
return asIs,toBe
def rows1(row,tbl,cells=lambda r: r.cells):
print ""
for h,cell in zip(tbl.headers,cells(row)):
print h.col, ") ", h.name,cell
def snakesAndLadders(tree,train,w):
def klass(x): return x.cells[train.klass[0].col]
def l2t(l) : return l.tbl
def xpect(tbl): return tbl.klass[0].centroid()
def score(l):
if callable(w):
return w(l)
if isinstance(w,dict):
return w[xpect(l2t(l))]
return l
for node in dtnodes(tree):
node.tbl = clone(train,
rows=map(lambda x:x.cells,node.rows),
keepSelections=True)
node.tbl.centroid= centroid(node.tbl,selections=True)
for node1 in dtnodes(tree):
id1 = node1._id
node1.far = []
node1.snake=None; node1.worse=[]
node1.ladder=None; node1.better=[]
for node2 in dtnodes(tree):
#if id1 > node2._id:
sames = overlap(node1.tbl.centroid, node2.tbl.centroid)
node1.far += [(sames,node2)]
#node2.far += [(sames,node1)]
for node1 in dtnodes(tree):
# sorted in reverse order of distance
node1.far = sorted(node1.far,
key= lambda x: first(x))
# at end of this loop, the last ladder, snakes are closest
for _,node2 in node1.far:
delta = prefer(node2.branch,node1.branch,key=lambda x:x.col)
if delta:
if score(node2) > score(node1):
node1.ladder = node2
node1.better = delta
if score(node2) < score(node1):
node1.snake = node2
node1.worse = delta
for node in dtnodes(tree):
snake = node.snake._id if node.snake else None
ladder = node.ladder._id if node.ladder else None
@demo
def tdived(file='data/diabetes.csv'):
tbl = discreteTable(file) # each row:[(0, 6), (100, 123), (0, 122), (0, 99), (14, 120), (26.4, 67.1), (0.528, 2.42), (21, 28), 'testednegative']
#exit()
tree= tdiv(tbl)
showTdiv(tree)
for node in dtnodes(tree):
rows = map(lambda x:x.cells,node.rows)
print rows
@demo
def tellme(file='data/trainingData.csv'):
tbl = discreteTable(file)
tree = tdiv(tbl)
showTdiv(tree)
@demo
def cross(file='data/housingD.csv',rseed=1):
def klass(test):
return test.cells[train.klass[0].col]
seed(rseed)
tbl = discreteTable(file)
n=0
abcd=Abcd()
nLeaves=Num()
nNodes=Num()
for tests, train in xval(tbl):
tree = tdiv(train)
for node in dtnodes(tree):
print node.branch
nLeaves + len([n for n in dtleaves(tree)])
nNodes + len([n for n in dtnodes(tree)])
for test in tests:
want = klass(test)
got = classify(test,tree)
abcd(want,got)
exit()
nl()
abcd.header()
abcd.report()
print ":nodes",sorted(nNodes.some.all())
print ":leaves",sorted(nLeaves.some.all())
# ninf = float("-inf")
@demo
def snl(file='data/poi-1.5D.csv',rseed=1,w=dict(_1=0,_0=1)):
def klass(x): return x.cells[train.klass[0].col]
def val((x,y)):
return y if x == ninf else x
seed(rseed)
nl(); print "#",file
tbl = discreteTable(file)
tree0 = tdiv(tbl)
showTdiv(tree0); nl()
old, better, worse = Sym(), Sym(), Sym()
abcd1, abcd2 = Abcd(db=file,rx="where"), Abcd(db=file,rx="ranfor")
abcd3 = Abcd(db=file, rx="logref")
abcd4 = Abcd(db=file, rx="dt")
abcd5 = Abcd(db=file, rx="nb")
for tests, train in xval(tbl):
learns(tests,train._rows,
indep=lambda row: map(val,row.cells[:-2]),
dep = lambda row: row.cells[-1],
rf = abcd2,
lg = abcd3,
dt = abcd4,
nb = abcd5),
tree = tdiv(train)
snakesAndLadders(tree,train,w)
for test in tests:
abcd1(actual = klass(test),
predicted = classify(test,tree))
a,b = improve(test,tree); old + a; better + b
_,c = degrade(test,tree); worse + c
print "\n:asIs",old.counts
print ":plan",better.counts
print ":warn",worse.counts
abcd1.header()
abcd1.report()
abcd2.report()
abcd3.report()
abcd4.report()
abcd5.report()
if __name__ == '__main__': eval(cmd())