forked from ryanbressler/CloudForest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tree.go
433 lines (360 loc) · 10.7 KB
/
tree.go
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package CloudForest
type nodeAndCases struct {
n *Node
start int
end int
nconstants int
parent *Node
dead bool
}
//Tree represents a single decision tree.
type Tree struct {
//Tree int
Root *Node
Target string
Weight float64
}
//NewTree initializes one node tree.
func NewTree() *Tree {
return &Tree{new(Node), "", -1.0}
}
func (t *Tree) Copy() *Tree {
if t == nil {
return nil
}
var root *Node
if t.Root != nil {
root = t.Root.Copy()
}
return &Tree{
Target: t.Target,
Weight: t.Weight,
Root: root,
}
}
//AddNode adds a node a the specified path with the specified pred value and/or
//Splitter. Paths are specified in the same format as in rf-aces sf files, as a
//string of 'L' and 'R'. Nodes must be added from the root up as the case where
//the path specifies a node whose parent does not already exist in the tree is
//not handled well.
func (t *Tree) AddNode(path string, pred string, splitter *Splitter) {
n := new(Node)
n.Pred = pred
n.Splitter = splitter
if t.Root == nil {
t.Root = n
} else {
loc := t.Root
for i := 0; i < len(path); i++ {
switch path[i : i+1] {
case "L":
if loc.Left == nil {
loc.Left = n
}
loc = loc.Left
case "R":
if loc.Right == nil {
loc.Right = n
}
loc = loc.Right
case "M":
if loc.Missing == nil {
loc.Missing = n
}
loc = loc.Missing
}
}
}
}
//StripCodes removes all of the coded splits from a tree so that it can be used on new catagorical data.
func (t *Tree) StripCodes() {
t.Root.Climb(func(n *Node) {
if n.CodedSplit != nil {
n.CodedSplit = nil
}
})
}
/*
Grow grows the receiver tree through recursion. It uses impurity decrease to select splitters at
each node as in Brieman's Random Forest. It should be called on a tree with only a root node defined.
fm is a feature matrix of training data.
target is the feature to predict via regression or classification as determined by feature type.
cases specifies the cases to calculate impurity decrease over and can contain repeated values
to allow for sampling of cases with replacement as in RF.
canidates specifies the potential features to use as splitters
mTry specifies the number of candidate features to evaluate for each split.
leafSize specifies the minimum number of cases at a leafNode.
splitmissing indicates if missing values should be split onto a third branch
vet indicates if splits should be penalized against a randomized version of them selves
*/
func (t *Tree) Grow(fm *FeatureMatrix,
target Target,
cases []int,
candidates []int,
oob []int,
mTry int,
leafSize int,
maxDepth int,
splitmissing bool,
force bool,
vet bool,
evaloob bool,
extraRandom bool,
importance *[]*RunningMean,
depthUsed *[]int,
allocs *BestSplitAllocs) {
//var innercanidates []int
var impDec float64
// for i := 0; i < len(allocs.Weights); i++ {
// allocs.Weights[i] = 0
// }
// allocs.Cases = allocs.Cases[0:0]
// for _, i := range cases {
// if allocs.Weights[i] == 0 {
// allocs.Cases = append(allocs.Cases, i)
// }
// allocs.Weights[i]++
// }
t.Root.CodedRecurse(func(n *Node, innercases *[]int, depth int, nconstantsbefore int) (fi int, split interface{}, nconstants int) {
nconstants = nconstantsbefore
if (depth < maxDepth || maxDepth <= 0) && (2*leafSize) <= len(*innercases) {
//SampleFirstN(&candidates, &innercanidates, mTry, 0)
//innercanidates = candidates[:mTry]
fi, split, impDec, nconstants = fm.BestSplitter(target, innercases, &candidates, mTry, &oob, leafSize, force, vet, evaloob, extraRandom, allocs, nconstantsbefore)
// for i := mTry; i < len(candidates)-1 && impDec == minImp; i++ {
// randi := i + rand.Intn(len(candidates)-i)
// candidates[randi], candidates[i] = candidates[i], candidates[randi]
// innercanidates = candidates[i : i+1]
// fi, split, impDec, nconstants = fm.BestSplitter(target, innercases, &innercanidates, &oob, leafSize, vet, evaloob, allocs, nconstantsbefore)
// }
if split != nil {
if importance != nil {
(*importance)[fi].Add(impDec)
}
if depthUsed != nil && ((*depthUsed)[fi] == 0 || depth < (*depthUsed)[fi]) {
(*depthUsed)[fi] = depth
}
//not a leaf node so define the splitter and left and right nodes
//so recursion will continue
n.CodedSplit = split
n.Featurei = fi
n.Splitter = fm.Data[fi].DecodeSplit(split)
n.Pred = ""
//is this check needed? is it safe to reuse?
if n.Left == nil || n.Right == nil {
n.Left = new(Node)
n.Right = new(Node)
}
if splitmissing {
n.Missing = new(Node)
}
return
}
}
//fmt.Println("Terminating in tree grow.")
//Leaf node so find the predictive value and set it in n.Pred
split = nil
n.CodedSplit = nil
n.Splitter = nil
n.Pred = target.FindPredicted(*innercases)
return
}, fm, &cases, 0, 0)
}
func (t *Tree) GrowJungle(fm *FeatureMatrix,
target Target,
cases []int,
candidates []int,
oob []int,
mTry int,
leafSize int,
maxDepth int,
splitmissing bool,
force bool,
vet bool,
evaloob bool,
extraRandom bool,
importance *[]*RunningMean,
depthUsed *[]int,
allocs *BestSplitAllocs) {
//var innercanidates []int
var impDec float64
nodes := []nodeAndCases{{t.Root, 0, len(cases), 0, nil, false}}
var depth, nconstants, start, end, fi, firstThisLevel int
var split interface{}
innercases := cases[0:]
innercases2 := cases[0:]
for {
//Extend Nodes
lastThisLevel := len(nodes)
for i := firstThisLevel; i < lastThisLevel; i++ {
if nodes[i].dead {
continue
}
node := nodes[i]
n := node.n
start = node.start
end = node.end
innercases = cases[node.start:node.end]
nconstants = node.nconstants
if (depth < maxDepth || maxDepth <= 0) && (2*leafSize) <= len(innercases) {
fi, split, impDec, nconstants = fm.BestSplitter(target, &innercases, &candidates, mTry, &oob, leafSize, force, vet, evaloob, extraRandom, allocs, nconstants)
if split != nil {
if importance != nil {
(*importance)[fi].Add(impDec)
}
if depthUsed != nil && ((*depthUsed)[fi] == 0 || depth < (*depthUsed)[fi]) {
(*depthUsed)[fi] = depth
}
//not a leaf node so define the splitter and left and right nodes
//so recursion will continue
n.CodedSplit = split
n.Featurei = fi
n.Splitter = fm.Data[fi].DecodeSplit(split)
n.Pred = ""
li, ri := fm.Data[fi].SplitPoints(split, &innercases)
//Left
n.Left = new(Node)
nodes = append(nodes, nodeAndCases{n.Left, start, start + li, nconstants, n, false})
//Right
n.Right = new(Node)
nodes = append(nodes, nodeAndCases{n.Right, start + ri, end, nconstants, n, false})
if splitmissing {
n.Missing = new(Node)
if li != ri {
nodes = append(nodes, nodeAndCases{n.Missing, start + li, start + ri, nconstants, n, false})
}
}
continue
}
}
//Leaf node so find the predictive value and set it in n.Pred
split = nil
n.CodedSplit = nil
n.Splitter = nil
n.Pred = target.FindPredicted(innercases)
continue
}
if len(nodes) == lastThisLevel {
break
}
//Combine next level nodes here for jungles
madeChanges := true
for madeChanges {
madeChanges = false
for i := lastThisLevel; i < len(nodes); i++ {
if nodes[i].dead {
continue
}
var maxImpDec, impDec float64
combine := -1
nodei := nodes[i]
for j := lastThisLevel; j < len(nodes); j++ {
if nodes[j].dead {
continue
}
nodej := nodes[j]
if nodei.end == nodej.start && nodei.parent != nodej.parent {
//should we consider other harder noncontigeous combinations?
//or is theis enough of a random search to be fine?
innercases = cases[nodei.start:nodei.end]
innercases2 = cases[nodej.start:nodej.end]
impDec = target.SplitImpurity(&innercases, &innercases2, nil, allocs)
innercases = cases[nodei.start:nodej.end]
impDec -= target.Impurity(&innercases, allocs.Counter)
if impDec > maxImpDec {
maxImpDec = impDec
combine = j
}
}
}
if combine != -1 {
madeChanges = true
nodes[combine].dead = true
nj := nodes[combine]
nodei.end = nj.end
parent := nj.parent
switch nj.n {
case parent.Left:
parent.Left = nodei.n
case parent.Right:
parent.Right = nodei.n
case parent.Missing:
parent.Missing = nodei.n
}
}
}
}
firstThisLevel = lastThisLevel
depth++
}
}
//GetLeaves is called by the leaf count utility to
//gather statistics about the nodes of a tree including the sets of cases at
//"leaf" nodes that aren't split further and the number of times each feature
//is used to split away each case.
func (t *Tree) GetLeaves(fm *FeatureMatrix, fbycase *SparseCounter) []Leaf {
leaves := make([]Leaf, 0)
ncases := fm.Data[0].Length()
cases := makeCases(ncases)
t.Root.Recurse(func(n *Node, cases []int, depth int) {
if n.Left == nil && n.Right == nil { // I'm in a leaf node
leaves = append(leaves, Leaf{cases, n.Pred})
}
if fbycase != nil && n.Splitter != nil { //I'm not in a leaf node?
for _, c := range cases {
fbycase.Add(c, fm.Map[n.Splitter.Feature], 1)
}
}
}, fm, cases, 0)
return leaves
}
//Partition partitions all of the cases in a FeatureMatrix.
func (t *Tree) Partition(fm *FeatureMatrix) (*[][]int, *[]string) {
leaves := make([][]int, 0)
preds := make([]string, 0)
ncases := fm.Data[0].Length()
cases := makeCases(ncases)
t.Root.Recurse(func(n *Node, cases []int, depth int) {
if n.Left == nil && n.Right == nil { // I'm in a leaf node
leaves = append(leaves, cases)
preds = append(preds, n.Pred)
}
}, fm, cases, 0)
return &leaves, &preds
}
//Leaf is a struct for storing the index of the cases at a terminal "Leaf" node
//along with the Numeric predicted value.
type Leaf struct {
Cases []int
Pred string
}
//Vote casts a vote for the predicted value of each case in fm *FeatureMatrix.
//into bb *BallotBox. Since BallotBox is not thread safe trees should not vote
//into the same BallotBox in parallel.
func (t *Tree) Vote(fm *FeatureMatrix, bb VoteTallyer) {
t.VoteCases(fm, bb, makeCases(fm.Data[0].Length()))
}
//VoteCases casts a vote for the predicted value of each case in fm *FeatureMatrix.
//into bb *BallotBox. Since BallotBox is not thread safe trees should not vote
//into the same BallotBox in parallel.
func (t *Tree) VoteCases(fm *FeatureMatrix, bb VoteTallyer, cases []int) {
weight := 1.0
if t.Weight >= 0.0 {
weight = t.Weight
}
t.Root.Recurse(func(n *Node, cases []int, depth int) {
if n.Left == nil && n.Right == nil {
// I'm in a leaf node
for i := 0; i < len(cases); i++ {
bb.Vote(cases[i], n.Pred, weight)
}
}
}, fm, cases, 0)
}
func makeCases(n int) []int {
cases := make([]int, n)
for i := range cases {
cases[i] = i
}
return cases
}