-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathregression_tree.go
586 lines (526 loc) · 15.3 KB
/
regression_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
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
package gbdt
import (
"container/list"
"fmt"
"log"
"math"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
)
var _ = os.Exit
var _ = fmt.Println
var _ = time.Now
const (
LEFT = 0
RIGHT = 1
UNKNOWN = 2
CHILDSIZE = 3
UNKNOWN_VALUE = -math.MaxFloat32
)
type Node struct {
child []*Node
feature_split Feature
isleaf bool
pred float32
variance float32
sample_count int //number of sample at this node
depth int
}
type RegressionTree struct {
root *Node
max_depth int
min_leaf_size int
}
type NodeSample struct {
node *Node
sample_sequence []int //sample sequence thie node included
}
type FeatureSplitInfo struct {
feature_split Feature
variance float32
}
func NewRegressionTree() *RegressionTree {
return &RegressionTree{
root: nil,
max_depth: Conf.Max_depth,
min_leaf_size: Conf.Min_leaf_size,
}
}
func (self *RegressionTree) Fit(d *DataSet, l int) {
if l > len(d.Samples) {
log.Fatal("data length out of index")
}
self.root = &Node{
child: nil,
isleaf: false,
pred: 0,
variance: 0,
sample_count: l,
depth: 0,
}
//feature sampling
featureid_list := make([]int, Conf.Number_of_feature)
sampled_feature := make(map[int]bool)
for i := 0; i < len(featureid_list); i++ {
featureid_list[i] = i
sampled_feature[i] = false
}
if Conf.Feature_sampling_ratio < 1 {
random_shuffle(featureid_list, len(featureid_list)) //sample features for fitting tree
}
k := int(Conf.Feature_sampling_ratio * float32(Conf.Number_of_feature))
for i := 0; i < k; i++ {
sampled_feature[featureid_list[i]] = true
}
ns := &NodeSample{}
ns.sample_sequence = make([]int, l)
for i, _ := range d.Samples {
if i >= l {
break
}
ns.sample_sequence[i] = i
}
ns.node = self.root
queue := list.New()
queue.PushBack(ns)
predepth := 0
for queue.Len() != 0 {
var wg sync.WaitGroup
predepth = queue.Front().Value.(*NodeSample).node.depth
for queue.Len() != 0 {
temp_ns := queue.Front()
temp := temp_ns.Value.(*NodeSample)
depth := temp.node.depth
if predepth != depth {
break
}
wg.Add(1)
go func(d *DataSet, node *Node, sample_sequence []int, queue *list.List, sampled_feature map[int]bool) {
self.FitTree(d, node, sample_sequence, queue, sampled_feature)
wg.Done()
}(d, temp.node, temp.sample_sequence, queue, sampled_feature)
queue.Remove(temp_ns)
}
wg.Wait()
}
}
func (self *RegressionTree) FitTree(d *DataSet, node *Node, sample_sequence []int, queue *list.List, sampled_feature map[int]bool) {
node.pred = NodePredictValue(d, sample_sequence)
node.variance = CalculateVariance(d, sample_sequence)
if node.depth >= self.max_depth || node.sample_count <= self.min_leaf_size || SameTarget(d, sample_sequence) {
node.isleaf = true
//node.feature_split.Id = -1
if node.depth == 0 {
log.Println("same target!")
}
return
}
if self.FindSplitFeature(d, node, sample_sequence, sampled_feature) == false {
node.isleaf = true
if node.depth == 0 {
log.Println("can't find split feature!")
}
//node.feature_split.Id = -1
return
}
child_sample_sequence := make([][]int, CHILDSIZE)
index := node.feature_split.Id
split_value := node.feature_split.Value
for _, k := range sample_sequence {
{
if val := d.Samples[k].Features[index]; val == UNKNOWN_VALUE { //fix me!
child_sample_sequence[UNKNOWN] = append(child_sample_sequence[UNKNOWN], k)
} else {
if val < split_value {
child_sample_sequence[LEFT] = append(child_sample_sequence[LEFT], k)
} else {
child_sample_sequence[RIGHT] = append(child_sample_sequence[RIGHT], k)
}
}
}
}
node.child = make([]*Node, CHILDSIZE)
if len(child_sample_sequence[LEFT]) < self.min_leaf_size || len(child_sample_sequence[RIGHT]) < self.min_leaf_size {
if Conf.Enable_feature_tunning && len(Conf.Feature_costs) == Conf.Number_of_feature && node.depth == 0 {
Conf.Feature_costs[node.feature_split.Id] += 1e-4
}
if node.depth == 0 {
log.Println("child number too small!")
}
node.isleaf = true
//node.feature_split.Id = -1
return
}
if Conf.Enable_feature_tunning && len(Conf.Feature_costs) == Conf.Number_of_feature {
Conf.Feature_costs[node.feature_split.Id] += 1e-4
}
node.child[LEFT] = &Node{child: nil, isleaf: false, pred: 0, variance: 0, sample_count: len(child_sample_sequence[LEFT]), depth: node.depth + 1}
queue.PushBack(&NodeSample{node: node.child[LEFT], sample_sequence: child_sample_sequence[LEFT]})
node.child[RIGHT] = &Node{child: nil, isleaf: false, pred: 0, variance: 0, sample_count: len(child_sample_sequence[RIGHT]), depth: node.depth + 1}
queue.PushBack(&NodeSample{node: node.child[RIGHT], sample_sequence: child_sample_sequence[RIGHT]})
if len(child_sample_sequence[UNKNOWN]) > self.min_leaf_size {
node.child[UNKNOWN] = &Node{child: nil, isleaf: false, pred: 0, variance: 0, sample_count: len(child_sample_sequence[UNKNOWN]), depth: node.depth + 1}
queue.PushBack(&NodeSample{node: node.child[UNKNOWN], sample_sequence: child_sample_sequence[UNKNOWN]})
}
}
func (self *RegressionTree) FindSplitFeature(d *DataSet, node *Node, sample_sequence []int, sampled_feature map[int]bool) bool {
feature_tuple_list := make(map[int]*TupleList)
for _, index := range sample_sequence { //build index for feature to samples
for fid, fvalue := range d.Samples[index].Features {
if val, ok := sampled_feature[fid]; ok && val == true {
if _, ok2 := feature_tuple_list[fid]; !ok2 {
feature_tuple_list[fid] = NewTupleList()
}
feature_tuple_list[fid].AddTuple(fvalue, d.Samples[index].target, d.Samples[index].weight)
}
}
}
node.feature_split = Feature{Id: -1, Value: 0.0}
var min_variance float32 = math.MaxFloat32
var wg sync.WaitGroup
chan_feature_split := make(chan *FeatureSplitInfo, len(feature_tuple_list)) //channel length
for fid, t := range feature_tuple_list { //find the best feature to split Node with
wg.Add(1) //goroutine counter add 1
go func(id int, tl *TupleList) {
sort.Sort(tl)
feature_split_info, ok := self.GetFeatureSplitValue(id, tl)
if ok {
chan_feature_split <- feature_split_info
}
wg.Done() //goroutine counter minus 1 when function return
}(fid, t)
}
wg.Wait() //Wait blocks until the WaitGroup counter is zero
close(chan_feature_split)
for v := range chan_feature_split {
if v != nil {
temp_feature_split := v
if min_variance > temp_feature_split.variance {
min_variance = temp_feature_split.variance
node.feature_split = temp_feature_split.feature_split
}
}
}
return min_variance != math.MaxFloat32
}
func (self *RegressionTree) GetFeatureSplitValue(fid int, t *TupleList) (*FeatureSplitInfo, bool) {
var local_min_variance float32 = math.MaxFloat32
var split_value float32 = 0.0
var unknown int = 0
var s, ss, total_weight float64 = 0.0, 0.0, 0.0
var variance1, variance2, variance3 float32 = 0.0, 0.0, 0.0
l := len(t.tuplelist)
for unknown < l && t.tuplelist[unknown].value == UNKNOWN_VALUE { //calculate variance of unknown value samples for this feature
s += float64(t.tuplelist[unknown].target * t.tuplelist[unknown].weight)
ss += float64(t.tuplelist[unknown].target * t.tuplelist[unknown].target * t.tuplelist[unknown].weight)
total_weight += float64(t.tuplelist[unknown].weight)
unknown++
}
if unknown == l {
return nil, false
}
if total_weight > 1 {
variance1 = float32(ss - s*s/total_weight)
} else {
variance1 = 0
}
if variance1 < 0 {
fmt.Println("variance1<0 for fid=", fid)
variance1 = 0
}
s, ss, total_weight = 0, 0, 0
for i := unknown; i < l; i++ {
s += float64(t.tuplelist[i].target * t.tuplelist[i].weight)
ss += float64(t.tuplelist[i].target * t.tuplelist[i].target * t.tuplelist[i].weight)
total_weight += float64(t.tuplelist[i].weight)
}
var ls, lss, left_total_weight float64 = 0, 0, 0
var rs, rss, right_total_weight float64 = s, ss, total_weight
for i := unknown; i < l-1; i++ {
s = float64(t.tuplelist[i].target * t.tuplelist[i].weight)
ss = float64(t.tuplelist[i].target * t.tuplelist[i].target * t.tuplelist[i].weight)
total_weight = float64(t.tuplelist[i].weight)
ls += s
lss += ss
left_total_weight += total_weight
rs -= s
rss -= ss
right_total_weight -= total_weight
val1, val2 := t.tuplelist[i].value, t.tuplelist[i+1].value
if Float32Equal(val1, val2) {
continue
}
if left_total_weight > 1 {
variance2 = float32(lss - ls*ls/left_total_weight)
} else {
variance2 = 0
}
if variance2 < 0 {
//fmt.Println("variance2<0 for i=", i)
variance2 = 0
}
if right_total_weight > 1 {
variance3 = float32(rss - rs*rs/right_total_weight)
} else {
variance3 = 0
}
if variance3 < 0 {
variance3 = 0
}
variance := variance1 + variance2 + variance3
if Conf.Enable_feature_tunning && len(Conf.Feature_costs) == Conf.Number_of_feature {
variance *= Conf.Feature_costs[fid]
}
if local_min_variance > variance {
local_min_variance = variance
split_value = (val1 + val2) / 2.0
}
}
feature_split_info := &FeatureSplitInfo{
feature_split: Feature{Id: fid, Value: split_value},
variance: local_min_variance,
}
return feature_split_info, local_min_variance != math.MaxFloat32
}
func (self *RegressionTree) Predict(sample *Sample) float32 {
node := self.root
for {
if node.isleaf {
return node.pred
}
fid := node.feature_split.Id
split_value := node.feature_split.Value
if sample.Features[fid] == UNKNOWN_VALUE {
if node.child[UNKNOWN] != nil {
node = node.child[UNKNOWN]
} else {
return node.pred
}
} else {
if sample.Features[fid] < split_value {
node = node.child[LEFT]
} else {
node = node.child[RIGHT]
}
}
}
}
func (self *RegressionTree) GetFeatureCombine(sample *Sample) string {
node := self.root
val := ""
for {
if node.isleaf {
return val
}
fid := node.feature_split.Id
split_value := node.feature_split.Value
if sample.Features[fid] == UNKNOWN_VALUE {
if node.child[UNKNOWN] != nil {
node = node.child[UNKNOWN]
val += "u"
} else {
return val
}
} else {
if sample.Features[fid] < split_value {
node = node.child[LEFT]
val += "l"
} else {
node = node.child[RIGHT]
val += "r"
}
}
}
}
func (self *RegressionTree) GetSampleFeatureWeight(sample *Sample, sample_feature_weight map[int]float32) {
node := self.root
for {
last_level_pred := node.pred
if node.isleaf {
return
}
fid := node.feature_split.Id
split_value := node.feature_split.Value
if sample.Features[fid] == UNKNOWN_VALUE {
if node.child[UNKNOWN] != nil {
node = node.child[UNKNOWN]
} else {
return
}
} else {
if sample.Features[fid] < split_value {
node = node.child[LEFT]
} else {
node = node.child[RIGHT]
}
score := node.pred - last_level_pred
sample_feature_weight[fid] += score
}
}
}
func (self *RegressionTree) Save() string {
queue := list.New()
position_map := make(map[*Node]int)
self.SaveNodePos(self.root, queue, position_map)
if queue.Len() == 0 {
return ""
}
vs := make([]string, 0)
for e := queue.Front(); e != nil; e = e.Next() {
node := e.Value.(*Node)
line := strconv.Itoa(position_map[node])
line += "\t"
line += strconv.Itoa(node.feature_split.Id)
line += "\t"
line += strconv.FormatFloat(float64(node.feature_split.Value), 'f', 4, 32)
line += "\t"
line += strconv.FormatBool(node.isleaf)
line += "\t"
line += strconv.FormatFloat(float64(node.pred), 'f', 4, 32)
line += "\t"
line += strconv.FormatFloat(float64(node.variance), 'f', 4, 32)
line += "\t"
line += strconv.Itoa(node.depth)
line += "\t"
line += strconv.Itoa(node.sample_count)
for i := 0; i < CHILDSIZE; i++ {
line += "\t"
if node.child != nil && node.child[i] != nil {
line += strconv.Itoa(position_map[node.child[i]])
} else {
line += "-1"
}
}
vs = append(vs, line)
}
return strings.Join(vs, "\n")
}
func (self *RegressionTree) SaveNodePos(node *Node, queue *list.List, position_map map[*Node]int) {
if node == nil {
return
}
queue.PushBack(node)
position_map[node] = queue.Len() - 1
for e := queue.Front(); e != nil; e = e.Next() {
temp_node := e.Value.(*Node)
if temp_node != nil {
for i := 0; i < CHILDSIZE; i++ {
if temp_node.child != nil && temp_node.child[i] != nil {
queue.PushBack(temp_node.child[i])
position_map[temp_node.child[i]] = queue.Len() - 1
}
}
}
}
}
func (self *RegressionTree) Load(s string) {
self.root = nil
vs := strings.Split(s, "\n")
left := make([]int, 0)
right := make([]int, 0)
unknown := make([]int, 0)
nodes := make([]*Node, 0)
for i := 0; i < len(vs); i++ {
items := strings.Split(vs[i], "\t")
node := &Node{}
node.child = make([]*Node, CHILDSIZE)
if id, err := strconv.ParseInt(items[1], 10, 0); err != nil {
log.Fatal("feature_split.id", err)
} else {
node.feature_split.Id = int(id)
}
if value, err := strconv.ParseFloat(items[2], 32); err != nil {
log.Fatal("feature_split.value", err)
} else {
node.feature_split.Value = float32(value)
}
if isleaf, err := strconv.ParseBool(items[3]); err != nil {
log.Fatal("isleaf", err)
} else {
node.isleaf = isleaf
}
if pred, err := strconv.ParseFloat(items[4], 32); err != nil {
log.Fatal("pred", err)
} else {
node.pred = float32(pred)
}
if variance, err := strconv.ParseFloat(items[5], 32); err != nil {
log.Fatal("variance", err)
} else {
node.variance = float32(variance)
}
if depth, err := strconv.ParseInt(items[6], 10, 0); err != nil {
log.Fatal("depth", err)
} else {
node.depth = int(depth)
}
if sample_count, err := strconv.ParseInt(items[7], 10, 0); err != nil {
log.Fatal("sample_count", err)
} else {
node.sample_count = int(sample_count)
}
nodes = append(nodes, node)
if lt, err := strconv.ParseInt(items[8], 10, 0); err != nil {
log.Fatal("left", err)
} else {
left = append(left, int(lt))
}
if rt, err := strconv.ParseInt(items[9], 10, 0); err != nil {
log.Fatal("right", err)
} else {
right = append(right, int(rt))
}
if un, err := strconv.ParseInt(items[10], 10, 0); err != nil {
log.Fatal("unknown", err)
} else {
unknown = append(unknown, int(un))
}
}
for i := 0; i < len(nodes); i++ {
if left[i] >= 0 {
nodes[i].child[LEFT] = nodes[left[i]]
}
if right[i] >= 0 {
nodes[i].child[RIGHT] = nodes[right[i]]
}
if unknown[i] >= 0 {
nodes[i].child[UNKNOWN] = nodes[unknown[i]]
}
}
self.root = nodes[0]
}
func (self *RegressionTree) GetTreeFeatureWeight() map[int]float32 {
feature_weight := make(map[int]float32)
queue := list.New()
if self.root != nil {
queue.PushBack(self.root)
}
for e := queue.Front(); e != nil; e = e.Next() {
node := e.Value.(*Node)
if node.isleaf == false {
queue.PushBack(node.child[LEFT])
queue.PushBack(node.child[RIGHT])
var gain float32 = 0.0
if node.child[UNKNOWN] != nil {
queue.PushBack(node.child[UNKNOWN])
gain = node.variance - node.child[LEFT].variance - node.child[RIGHT].variance - node.child[UNKNOWN].variance
} else {
gain = node.variance - node.child[LEFT].variance - node.child[RIGHT].variance
}
if val, ok := feature_weight[node.feature_split.Id]; ok {
if gain > val {
feature_weight[node.feature_split.Id] = gain
}
} else {
feature_weight[node.feature_split.Id] = gain
}
} else {
continue
}
}
return feature_weight
}