forked from ryanbressler/CloudForest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
densenumfeature_test.go
141 lines (105 loc) · 3.98 KB
/
densenumfeature_test.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
package CloudForest
import "testing"
func TestNumFeature(t *testing.T) {
name := "numfeature"
f := &DenseNumFeature{
make([]float64, 0, 0),
make([]bool, 0, 0),
name,
false}
f.Append("0.1")
f.Append("10.1")
f.Append("10.2")
if x := f.NCats(); x != 0 {
t.Errorf("Numerical NCats = %v != 0", x)
}
codedSplit := 0.5
cases := []int{0, 1, 2}
l, r, m := f.Split(codedSplit, cases)
if len(l) != 1 || len(r) != 2 || len(m) != 0 {
t.Errorf("After Coded Numerical Split Left, Right, Missing Lengths = %v %v %v not 1 2 0", len(l), len(r), len(m))
}
decodedsplit := f.DecodeSplit(codedSplit)
fm := FeatureMatrix{[]Feature{f},
map[string]int{name: 0},
[]string{name}}
if !f.GoesLeft(0, decodedsplit) {
t.Errorf("Value %v sent right by spliter decoded from %v", f.NumData[0], codedSplit)
}
if f.GoesLeft(1, decodedsplit) {
t.Errorf("Value %v sent left by spliter decoded from %v", f.NumData[1], codedSplit)
}
l, r, m = decodedsplit.Split(&fm, cases)
if len(l) != 1 || len(r) != 2 || len(m) != 0 {
t.Errorf("After Decoded Numerical Split Left, Right, Missing Lengths = %v %v %v not 1 2 0", len(l), len(r), len(m))
}
f.Append("0.0")
cases = append(cases, 3)
f.Append("0.0")
cases = append(cases, 4)
l, r, m = decodedsplit.Split(&fm, cases)
if len(l) != 3 || len(r) != 2 || len(m) != 0 {
t.Errorf("After Decoded Numerical Split Left, Right, Missing Lengths = %v %v %v not 3 2 0", len(l), len(r), len(m))
}
l, r, m = f.Split(codedSplit, cases)
if len(l) != 3 || len(r) != 2 || len(m) != 0 {
t.Errorf("After Coded Numerical Split Left, Right, Missing Lengths = %v %v %v not 3 2 0", len(l), len(r), len(m))
}
//check self slitting
allocs := NewBestSplitAllocs(5, f)
_, split, _, _ := fm.BestSplitter(f, &cases, &[]int{0}, 1, nil, 1, false, false, false, false, allocs, 0)
//fm.BestSplitter(target, cases, candidates, oob, leafSize, vet, evaloob, allocs)
if split.(float64) != 5.1 {
t.Errorf("Numerical feature didn't self split correctelly. Returned %v not 5.1", split)
}
l, r, m = f.Split(split, cases)
if len(l) != 3 || len(r) != 2 || len(m) != 0 {
t.Errorf("After Coded Numerical Split Left, Right, Missing Lengths = %v %v %v not 3 2 0", len(l), len(r), len(m))
}
//and with a run of equals
f.Append(".1")
cases = append(cases, 5)
f.Append(".1")
cases = append(cases, 6)
allocs = NewBestSplitAllocs(7, f)
_, split, _, constants := fm.BestSplitter(f, &cases, &[]int{0}, 1, nil, 1, false, false, false, false, allocs, 0)
//fm.BestSplitter(target, cases, candidates, oob, leafSize, vet, evaloob, allocs)
if split.(float64) != 5.1 || constants != 0 {
t.Errorf("Numerical feature didn't self split correctelly with equal run. Returned %v not 5.1", split)
}
l, r, m = f.Split(split, cases)
if len(l) != 5 || len(r) != 2 || len(m) != 0 {
t.Errorf("After Coded Numerical Split with equal run Left, Right, Missing Lengths = %v %v %v not 5 2 0", len(l), len(r), len(m))
}
//spliting between two runs of equals
f.Append("10.1")
cases = append(cases, 7)
allocs = NewBestSplitAllocs(8, f)
_, split, _, _ = fm.BestSplitter(f, &cases, &[]int{0}, 1, nil, 1, false, false, false, false, allocs, 0)
//fm.BestSplitter(target, cases, candidates, oob, leafSize, vet, evaloob, allocs)
sorted := true
for i := 1; i < len(cases); i++ {
if f.NumData[cases[i]] < f.NumData[cases[i-1]] {
sorted = false
}
}
if !sorted {
t.Error("Numerical feature didn't sort cases.")
}
if split.(float64) != 5.1 {
t.Errorf("Numerical feature didn't self split correctelly between equal runs. Returned %v not 5.1", split)
}
l, r, m = f.Split(split, cases)
if len(l) != 5 || len(r) != 3 || len(m) != 0 {
t.Errorf("After Coded Numerical Split between equal runs Left, Right, Missing Lengths = %v %v %v not 5 3 0", len(l), len(r), len(m))
}
f = &DenseNumFeature{
make([]float64, 0, 0),
make([]bool, 0, 0),
name,
false}
f.Append("0")
if f.IsMissing(0) || !f.IsZero(0) {
t.Errorf("feature should not be missing (%v), and be of zero value (%v)", !f.IsMissing(0), f.IsZero(0))
}
}