forked from lineker/Random-Forests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treerandom.py
362 lines (306 loc) · 10.5 KB
/
treerandom.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
from decisionnode import *
import random
import treepredict
# Divides a set on a specific column. Can handle numeric
# or nominal values
def divideset(rows,column,value):
# Make a function that tells us if a row is in
# the first group (true) or the second group (false)
split_function=None
if isinstance(value,int) or isinstance(value,float):
split_function=lambda row:row[column]>=value
else:
split_function=lambda row:row[column]==value
# Divide the rows into two sets and return them
set1=[row for row in rows if split_function(row)]
set2=[row for row in rows if not split_function(row)]
return (set1,set2)
# Entropy is the sum of p(x)log(p(x)) across all
# the different possible results
def entropy(rows):
from math import log
log2=lambda x:log(x)/log(2)
results=uniquecounts(rows)
# Now calculate the entropy
ent=0.0
for r in results.keys():
p=float(results[r])/len(rows)
ent=ent-p*log2(p)
#print ent
return ent
# Create counts of possible results (the last column of
# each row is the result)
# unique counts based on the label
#eg. {'1': 152, '0': 168, '3': 177, '2': 185, '4': 59}
def uniquecounts(rows):
#print len(rows)
#pdb.set_trace()
results={}
for row in rows:
# The result is the last column
r=row[len(row)-1]
#print len(row)
if r not in results: results[r]=0
results[r]+=1
#print "unicounts size : " + str(len(results)) + "size rows:" + str(len(rows))
#print results
return results
#choose median as cutting point
def get_cutting_point(column_values):
values = column_values.keys()
values.sort()
#print values
return values[len(values)/2]
# Probability that a randomly placed item will
# be in the wrong category
def giniimpurity(rows):
total=len(rows)
counts=uniquecounts(rows)
imp=0
for k1 in counts:
p1=float(counts[k1])/total
for k2 in counts:
if k1==k2: continue
p2=float(counts[k2])/total
imp+=p1*p2
return imp
def pick_candidate_random(candidates, rows):
return random.choice(candidates)
def pick_candidate_entropy(candidates, rows):
#use method entropy to choose best candidate
current_score = entropy(rows)
best_candidate = random.choice(candidates)
best_gain=0.0
for candidate in candidates:
col = candidate[0]
value = candidate[1]
#split set based on the feature and value
(set1,set2)=divideset(rows,col,value)
# Information gain
p=float(len(set1))/len(rows)
gain=current_score-p*entropy(set1)-(1-p)*entropy(set2)
if gain>best_gain and len(set1)>0 and len(set2)>0:
best_gain=gain
best_candidate = candidate
#print best_candidate
return best_candidate
def pick_candidate_gini(candidates,rows):
current_score = giniimpurity(rows)
best_candidate = random.choice(candidates)
best_gain=0.0
for candidate in candidates:
col = candidate[0]
value = candidate[1]
#split set based on the feature and value
(set1,set2)=divideset(rows,col,value)
#print "current gini= "+str(current_score)
#print "gini1="+str(giniimpurity(set1)) + " gini2="+str(giniimpurity(set2))
# Information gain
p=float(len(set1))/len(rows)
gain=current_score-p*giniimpurity(set1)-(1-p)*giniimpurity(set2)
if gain>best_gain and len(set1)>0 and len(set2)>0:
best_gain=gain
best_candidate = candidate
#print best_candidate
return best_candidate
def pick_candidate_gini_overall(candidates,rows):
current_score = giniimpurity(rows)
best_candidate = random.choice(candidates)
best_gain=0.0
for candidate in candidates:
col = candidate[0]
for value in candidate[1]:
#value = candidate[1]
#split set based on the feature and value
(set1,set2)=divideset(rows,col,value)
#print "current gini= "+str(current_score)
#print "gini1="+str(giniimpurity(set1)) + " gini2="+str(giniimpurity(set2))
# Information gain
p=float(len(set1))/len(rows)
gain=current_score-p*giniimpurity(set1)-(1-p)*giniimpurity(set2)
if gain>best_gain and len(set1)>0 and len(set2)>0:
best_gain=gain
best_candidate = candidate
#print best_candidate
return best_candidate
def get_voting_result(rows):
winner_key = 0
winner_value = 0
pickrandom = False
counts = uniquecounts(rows)
for key in counts.keys():
if(counts[key] > winner_value):
winner_key = key
winner_value = counts[key]
elif(counts[key] == winner_value):
pickrandom = True
if(pickrandom):
winner_key = random.choice(counts.keys())
dic = {winner_key:winner_value}
return dic
#rows = training data
#kcandidates = number of candidates to pick
#nmin number of examples a node need to have.
#scoref = function used to choose candidates
def buildrandomtree_gini_overall(rows,kcandidates,nmin,pickcandidate=pick_candidate_gini):
rows = rows[:]
if len(rows)==0: return decisionnode()
candidates = []
column_count=len(rows[0])-1
#print "number of columns = " + str(column_count)
#pick k random candidates
#candidate = (column_index,value)
for i in range(0,kcandidates):
random_index = random.randint(0,column_count-1)
#get all unique values for a specific feature (column)
column_values={}
for row in rows:
column_values[row[random_index]]=1
#get a cutting point
cutting_point = column_values.keys()
#print "rand feature index ="+str(random_index)+ "\n cutting point="+str(cutting_point)
#add to list of candidates
candidates.append((random_index,cutting_point))
#print candidates
#choose a candidate based on function given
chosen_candidate = pick_candidate_gini_overall(candidates,rows)
#print chosen_candidate
col = chosen_candidate[0]
value = chosen_candidate[1]
#split set based on the feature and value
(set1,set2)=divideset(rows,col,value)
#set1 = truebranch
trueBranch = None
#set2 = falsebranch
falseBranch = None
#print "item in leaf1 = " +str(len(set1))
#check if set1 has the min size
if(len(set1)<=nmin):
#do voting on the elements of of set1
#set and answer for this true branch
voting_result = get_voting_result(set1)
trueBranch = decisionnode(results=voting_result)
else:
#it means we need to grow this
trueBranch = buildrandomtree(set1,kcandidates,nmin,pickcandidate)
#print "item in leaf2 = " +str(len(set2))
#check if set2 has the min size
if(len(set2)<=nmin):
#do voting on the elements of of set2
#set and answer for this true branch
voting_result = get_voting_result(set2)
#uniquecounts
falseBranch = decisionnode(results=voting_result)
else:
#it means we need to grow this
falseBranch = buildrandomtree(set2,kcandidates,nmin,pickcandidate)
return decisionnode(col=col,value=value,tb=trueBranch,fb=falseBranch)
#rows = training data
#kcandidates = number of candidates to pick
#nmin number of examples a node need to have.
#scoref = function used to choose candidates
def buildrandomtree(rows,kcandidates,nmin,pickcandidate=pick_candidate_gini):
rows = rows[:]
if len(rows)==0: return decisionnode()
candidates = []
column_count=len(rows[0])-1
#print "number of columns = " + str(column_count)
#pick k random candidates
#candidate = (column_index,value)
for i in range(0,kcandidates):
random_index = random.randint(0,column_count-1)
#get all unique values for a specific feature (column)
column_values={}
for row in rows:
column_values[row[random_index]]=1
#get a cutting point
cutting_point = get_cutting_point(column_values)
#print "rand feature index ="+str(random_index)+ "\n cutting point="+str(cutting_point)
#add to list of candidates
candidates.append((random_index,cutting_point))
#print candidates
#choose a candidate based on function given
chosen_candidate = pickcandidate(candidates,rows)
#print chosen_candidate
col = chosen_candidate[0]
value = chosen_candidate[1]
#split set based on the feature and value
(set1,set2)=divideset(rows,col,value)
#set1 = truebranch
trueBranch = None
#set2 = falsebranch
falseBranch = None
#print "item in leaf1 = " +str(len(set1))
#check if set1 has the min size
if(len(set1)<=nmin):
#do voting on the elements of of set1
#set and answer for this true branch
voting_result = get_voting_result(set1)
trueBranch = decisionnode(results=voting_result)
else:
#it means we need to grow this
trueBranch = buildrandomtree(set1,kcandidates,nmin,pickcandidate)
#print "item in leaf2 = " +str(len(set2))
#check if set2 has the min size
if(len(set2)<=nmin):
#do voting on the elements of of set2
#set and answer for this true branch
voting_result = get_voting_result(set2)
#uniquecounts
falseBranch = decisionnode(results=voting_result)
else:
#it means we need to grow this
falseBranch = buildrandomtree(set2,kcandidates,nmin,pickcandidate)
return decisionnode(col=col,value=value,tb=trueBranch,fb=falseBranch)
def printtree(tree,indent=''):
# Is this a leaf node?
if tree.results!=None:
print str(tree.results)
else:
# Print the criteria
print 'row['+str(tree.col)+']>='+str(tree.value)+'? '
# Print the branches
print indent+'T->',
printtree(tree.tb,indent+' ')
print indent+'F->',
printtree(tree.fb,indent+' ')
"""
RANDOMIZED FOREST START HERE
"""
#this version receives a subset of features
def build_randomized_forest(rows,m,kcandidates,nmin,pickcandidate=pick_candidate_gini,callback=None):
forest = []
for i in range(0,m):
#tree = buildrandomtree(rows,kcandidates,nmin,pickcandidate=pickcandidate)
tree = buildrandomtree(rows,kcandidates,nmin,pickcandidate=pickcandidate)
forest.append(tree)
if callback: callback(i,m-i)
#print "size of forest = " + str(len(forest))
#print "building using pick random"
#for i in range(0,m/2):
# tree = buildrandomtree(rows,kcandidates,nmin,pickcandidate=pick_candidate_random)
# forest.append(tree)
# if callback: callback(i,m-i)
return forest
def classify(example,forest):
#print "using forest to classify"
counts = {}
#count the results
for i in range(0,len(forest)):
#printtree(forest[0])
r = treepredict.classify(example,forest[i])
if r not in counts: counts[r]=1
counts[r]+=1
winner_key = 0
winner_value = 0
pickrandom = False
for key in counts.keys():
if(counts[key] > winner_value):
winner_key = key
winner_value = counts[key]
elif (counts[key] == winner_value):
pickrandom = True
if(pickrandom):
winner_key = random.choice(counts.keys())
#print counts
return winner_key