forked from ryanbressler/CloudForest
/
grow.go
609 lines (513 loc) · 12.9 KB
/
grow.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package CloudForest
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"math"
"math/rand"
"os"
"regexp"
"runtime"
"runtime/pprof"
"sync"
"time"
)
type GrowOpts struct {
imp *string
costs *string
rfweights *string
blacklist *string
nCores int
StringnSamples string
StringmTry string
StringleafSize string
shuffleRE string
blockRE string
includeRE string
nTrees int
nContrasts int
cpuprofile *string
contrastAll bool
impute bool
splitmissing bool
l1 bool
density bool
vet bool
evaloob bool
force bool
entropy bool
oob bool
caseoob string
progress bool
adaboost bool
gradboost float64
multiboost bool
nobag bool
balance bool
balanceby string
ordinal bool
permutate bool
dotest bool
testfm string
}
func newEmpty() *string {
var s = ""
return &s
}
func (me *GrowOpts) SetDefaults() {
me.imp = newEmpty()
me.costs = newEmpty()
me.rfweights = newEmpty()
me.blacklist = newEmpty()
me.cpuprofile = newEmpty()
me.nCores = 1
me.StringnSamples = "0"
me.StringmTry = "0"
me.StringleafSize = "0"
me.nTrees = 100
}
func Grow(data *FeatureMatrix, forestwriter *ForestWriter, targetname *string, o GrowOpts) {
if *o.cpuprofile != "" {
f, err := os.Create(*o.cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
rand.Seed(time.Now().UTC().UnixNano())
if o.testfm != "" {
o.dotest = true
}
if o.multiboost {
fmt.Println("MULTIBOOST!!!!1!!!!1!!11 (things may break).")
}
var boostMutex sync.Mutex
boost := (o.adaboost || o.gradboost != 0.0)
if boost && !o.multiboost {
o.nCores = 1
}
if o.nCores > 1 {
runtime.GOMAXPROCS(o.nCores)
}
fmt.Printf("Threads : %v\n", o.nCores)
fmt.Printf("nTrees : %v\n", o.nTrees)
if o.nContrasts > 0 {
fmt.Printf("Adding %v Random Contrasts\n", o.nContrasts)
data.AddContrasts(o.nContrasts)
}
if o.contrastAll {
fmt.Printf("Adding Random Contrasts for All Features.\n")
data.ContrastAll()
}
blacklisted := 0
blacklistis := make([]bool, len(data.Data))
if *o.blacklist != "" {
fmt.Printf("Loading blacklist from: %v\n", *o.blacklist)
blackfile, err := os.Open(*o.blacklist)
if err != nil {
log.Fatal(err)
}
tsv := csv.NewReader(blackfile)
tsv.Comma = '\t'
for {
id, err := tsv.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
i, ok := data.Map[id[0]]
if !ok {
fmt.Printf("Ignoring blacklist feature not found in data: %v\n", id[0])
continue
}
if !blacklistis[i] {
blacklisted += 1
blacklistis[i] = true
}
}
blackfile.Close()
}
//find the target feature
fmt.Printf("Target : %v\n", *targetname)
targeti, ok := data.Map[*targetname]
if !ok {
log.Fatal("Target not found in data.")
}
if o.blockRE != "" {
re := regexp.MustCompile(o.blockRE)
for i, feature := range data.Data {
if targeti != i && re.MatchString(feature.GetName()) {
if blacklistis[i] == false {
blacklisted += 1
blacklistis[i] = true
}
}
}
}
if o.includeRE != "" {
re := regexp.MustCompile(o.includeRE)
for i, feature := range data.Data {
if targeti != i && !re.MatchString(feature.GetName()) {
if blacklistis[i] == false {
blacklisted += 1
blacklistis[i] = true
}
}
}
}
nFeatures := len(data.Data) - blacklisted - 1
fmt.Printf("Non Target Features : %v\n", nFeatures)
mTry := ParseAsIntOrFractionOfTotal(o.StringmTry, nFeatures)
if mTry <= 0 {
mTry = int(math.Ceil(math.Sqrt(float64(nFeatures))))
}
fmt.Printf("mTry : %v\n", mTry)
if o.impute {
fmt.Println("Imputing missing values to feature mean/mode.")
data.ImputeMissing()
}
if o.permutate {
fmt.Println("Permutating target feature.")
data.Data[targeti].Shuffle()
}
if o.shuffleRE != "" {
re := regexp.MustCompile(o.shuffleRE)
shuffled := 0
for i, feature := range data.Data {
if targeti != i && re.MatchString(feature.GetName()) {
data.Data[i].Shuffle()
shuffled += 1
}
}
fmt.Printf("Shuffled %v features matching %v\n", shuffled, o.shuffleRE)
}
targetf := data.Data[targeti]
unboostedTarget := targetf.Copy()
var bSampler Bagger
if o.balance {
bSampler = NewBalancedSampler(targetf.(*DenseCatFeature))
}
if o.balanceby != "" {
bSampler = NewSecondaryBalancedSampler(targetf.(*DenseCatFeature), data.Data[data.Map[o.balanceby]].(*DenseCatFeature))
o.balance = true
}
nNonMissing := 0
for i := 0; i < targetf.Length(); i++ {
if !targetf.IsMissing(i) {
nNonMissing += 1
}
}
fmt.Printf("non-missing cases: %v\n", nNonMissing)
leafSize := ParseAsIntOrFractionOfTotal(o.StringleafSize, nNonMissing)
if leafSize <= 0 {
if boost {
leafSize = nNonMissing / 3
} else if targetf.NCats() == 0 {
//regression
leafSize = 4
} else {
//classification
leafSize = 1
}
}
fmt.Printf("leafSize : %v\n", leafSize)
//infer nSamples and mTry from data if they are 0
nSamples := ParseAsIntOrFractionOfTotal(o.StringnSamples, nNonMissing)
if nSamples <= 0 {
nSamples = nNonMissing
}
fmt.Printf("nSamples : %v\n", nSamples)
if o.progress {
o.oob = true
}
if o.caseoob != "" {
o.oob = true
}
var oobVotes VoteTallyer
if o.oob {
fmt.Println("Recording oob error.")
if targetf.NCats() == 0 {
//regression
oobVotes = NewNumBallotBox(data.Data[0].Length())
} else {
//classification
oobVotes = NewCatBallotBox(data.Data[0].Length())
}
}
//****** Set up Target for Alternative Impurity if needed *******//
var target Target
if o.density {
fmt.Println("Estimating Density.")
target = &DensityTarget{&data.Data, nSamples}
} else {
switch targetf.(type) {
case NumFeature:
fmt.Println("Performing regression.")
if o.l1 {
fmt.Println("Using l1/absolute deviance error.")
targetf = &L1Target{targetf.(NumFeature)}
}
if o.ordinal {
fmt.Println("Using Ordinal (mode) prediction.")
targetf = NewOrdinalTarget(targetf.(NumFeature))
}
switch {
case o.gradboost != 0.0:
fmt.Println("Using Gradiant Boosting.")
targetf = &GradBoostTarget{targetf.(NumFeature), o.gradboost}
case o.adaboost:
fmt.Println("Using Numeric Adaptive Boosting.")
//BUG(ryan): gradiant boostign should expose learning rate.
targetf = NewNumAdaBoostTarget(targetf.(NumFeature))
}
target = targetf
case CatFeature:
fmt.Println("Performing classification.")
switch {
case *o.costs != "":
fmt.Println("Using missclasification costs: ", *o.costs)
costmap := make(map[string]float64)
err := json.Unmarshal([]byte(*o.costs), &costmap)
if err != nil {
log.Fatal(err)
}
regTarg := NewRegretTarget(targetf.(CatFeature))
regTarg.SetCosts(costmap)
targetf = regTarg
case *o.rfweights != "":
fmt.Println("Using rf weights: ", *o.rfweights)
weightmap := make(map[string]float64)
err := json.Unmarshal([]byte(*o.rfweights), &weightmap)
if err != nil {
log.Fatal(err)
}
wrfTarget := NewWRFTarget(targetf.(CatFeature), weightmap)
targetf = wrfTarget
case o.entropy:
fmt.Println("Using entropy minimization.")
targetf = &EntropyTarget{targetf.(CatFeature)}
case boost:
fmt.Println("Using Adaptive Boosting.")
targetf = NewAdaBoostTarget(targetf.(CatFeature))
}
target = targetf
}
}
//****************** Needed Collections and vars ******************//
var trees []*Tree
trees = make([]*Tree, 0, o.nTrees)
var imppnt *[]*RunningMean
var mmdpnt *[]*RunningMean
if *o.imp != "" {
fmt.Println("Recording Importance Scores.")
imppnt = NewRunningMeans(len(data.Data))
mmdpnt = NewRunningMeans(len(data.Data))
}
treechan := make(chan *Tree, 0)
//****************** Good Stuff Stars Here ;) ******************//
trainingStart := time.Now()
for core := 0; core < o.nCores; core++ {
go func() {
weight := -1.0
canidates := make([]int, 0, len(data.Data))
for i := 0; i < len(data.Data); i++ {
if i != targeti && !blacklistis[i] {
canidates = append(canidates, i)
}
}
tree := NewTree()
tree.Target = *targetname
cases := make([]int, 0, nSamples)
oobcases := make([]int, 0, nSamples)
if o.nobag {
for i := 0; i < nSamples; i++ {
if !targetf.IsMissing(i) {
cases = append(cases, i)
}
}
}
var depthUsed *[]int
if mmdpnt != nil {
du := make([]int, len(data.Data))
depthUsed = &du
}
allocs := NewBestSplitAllocs(nSamples, targetf)
for {
nCases := data.Data[0].Length()
//sample nCases case with replacement
if !o.nobag {
cases = cases[0:0]
if o.balance {
bSampler.Sample(&cases, nSamples)
} else {
for j := 0; len(cases) < nSamples; j++ {
r := rand.Intn(nCases)
if !targetf.IsMissing(r) {
cases = append(cases, r)
}
}
}
}
if o.nobag && nSamples != nCases {
cases = cases[0:0]
for i := 0; i < nSamples; i++ {
if !targetf.IsMissing(i) {
cases = append(cases, i)
}
}
SampleFirstN(&cases, nil, nCases, 0)
}
if o.oob || o.evaloob {
ibcases := make([]bool, nCases)
for _, v := range cases {
ibcases[v] = true
}
oobcases = oobcases[0:0]
for i, v := range ibcases {
if !v {
oobcases = append(oobcases, i)
}
}
}
tree.Grow(data, target, cases, canidates, oobcases, mTry, leafSize, o.splitmissing, o.force, o.vet, o.evaloob, imppnt, depthUsed, allocs)
if mmdpnt != nil {
for i, v := range *depthUsed {
if v != 0 {
(*mmdpnt)[i].Add(float64(v))
(*depthUsed)[i] = 0
}
}
}
if boost {
boostMutex.Lock()
weight = targetf.(BoostingTarget).Boost(tree.Partition(data))
boostMutex.Unlock()
if weight == math.Inf(1) {
fmt.Printf("Boosting Reached Weight of %v\n", weight)
close(treechan)
break
}
tree.Weight = weight
}
if o.oob {
tree.VoteCases(data, oobVotes, oobcases)
}
treechan <- tree
tree = <-treechan
}
}()
}
for i := 0; i < o.nTrees; i++ {
tree := <-treechan
if tree == nil {
break
}
if forestwriter != nil {
forestwriter.WriteTree(tree, i)
}
if o.dotest {
trees = append(trees, tree)
if i < o.nTrees-1 {
//newtree := new(Tree)
treechan <- NewTree()
}
} else {
if i < o.nTrees-1 {
treechan <- tree
}
}
if o.progress {
fmt.Printf("Model oob error after tree %v : %v\n", i, oobVotes.TallyError(unboostedTarget))
}
}
trainingEnd := time.Now()
fmt.Printf("Training model took %v.\n", trainingEnd.Sub(trainingStart))
if o.oob {
fmt.Printf("Out of Bag Error : %v\n", oobVotes.TallyError(unboostedTarget))
}
if o.caseoob != "" {
caseoobfile, err := os.Create(o.caseoob)
if err != nil {
log.Fatal(err)
}
defer caseoobfile.Close()
for i := 0; i < unboostedTarget.Length(); i++ {
fmt.Fprintf(caseoobfile, "%v\t%v\t%v\n", data.CaseLabels[i], oobVotes.Tally(i), unboostedTarget.GetStr(i))
}
}
if *o.imp != "" {
impfile, err := os.Create(*o.imp)
if err != nil {
log.Fatal(err)
}
defer impfile.Close()
for i, v := range *imppnt {
mean, count := v.Read()
meanMinDepth, treeCount := (*mmdpnt)[i].Read()
fmt.Fprintf(impfile, "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", data.Data[i].GetName(), mean, count, mean*float64(count)/float64(o.nTrees), mean*float64(count)/float64(treeCount), treeCount, meanMinDepth)
}
}
if o.dotest {
var bb VoteTallyer
testdata := data
testtarget := unboostedTarget
if o.testfm != "" {
var err error
testdata, err = LoadAFM(o.testfm)
if err != nil {
log.Fatal(err)
}
targeti, ok = testdata.Map[*targetname]
if !ok {
log.Fatal("Target not found in test data.")
}
testtarget = testdata.Data[targeti]
for _, tree := range trees {
// tree.Root.Climb(func(n *Node) {
// if n.Splitter == nil && n.CodedSplit != nil {
// fmt.Printf("node %v \n %v nil should be %v \n", *n, n.Splitter, n.CodedSplit)
// }
// if n.Splitter != nil && n.CodedSplit == nil {
// fmt.Printf("node %v \n %v nil should be %v \n", *n, n.Splitter, n.CodedSplit)
// }
// switch n.CodedSplit.(type) {
// case float64:
// v := n.Splitter.Value
// if n.CodedSplit.(float64) != v {
// fmt.Printf("%v splits not equal.\n", *n)
// }
// if n.Featurei != testdata.Map[n.Splitter.Feature] {
// fmt.Printf("Feature %v at %v not at %v \n", n.Splitter.Feature, testdata.Map[n.Splitter.Feature], n.Featurei)
// }
// }
// })
tree.StripCodes()
}
}
if unboostedTarget.NCats() == 0 {
//regression
bb = NewNumBallotBox(testdata.Data[0].Length())
} else {
//classification
bb = NewCatBallotBox(testdata.Data[0].Length())
}
for _, tree := range trees {
tree.Vote(testdata, bb)
}
fmt.Printf("Error: %v\n", bb.TallyError(testtarget))
if testtarget.NCats() != 0 {
correct := 0
length := testtarget.Length()
for i := 0; i < length; i++ {
if bb.Tally(i) == testtarget.GetStr(i) {
correct++
}
}
fmt.Printf("Classified: %v / %v = %v\n", correct, length, float64(correct)/float64(length))
}
}
}