-
Notifications
You must be signed in to change notification settings - Fork 73
/
lgensemble_test.go
382 lines (335 loc) · 9.95 KB
/
lgensemble_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
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
package leaves
import (
"bufio"
"math"
"os"
"path/filepath"
"testing"
"github.com/dmitryikh/leaves/mat"
"github.com/dmitryikh/leaves/util"
)
func TestReadLGTree(t *testing.T) {
path := filepath.Join("testdata", "model_simple.txt")
reader, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
bufReader := bufio.NewReader(reader)
// Read ensemble header (to skip)
_, err = util.ReadParamsUntilBlank(bufReader)
if err != nil {
t.Fatal(err)
}
// Read first tree only
tree, err := lgTreeFromReader(bufReader)
if err != nil {
t.Fatal(err)
}
if len(tree.nodes) != 2 {
t.Fatalf("tree.nodes != 2 (got %d)", len(tree.nodes))
}
if tree.nCategorical != 1 {
t.Fatalf("tree.nCategorical != 1 (got %d)", tree.nCategorical)
}
trueLeavesValues := []float64{0.56697267424823339, 0.3584987837673016, 0.41213915936587919}
if err := util.AlmostEqualFloat64Slices(tree.leafValues, trueLeavesValues, 1e-10); err != nil {
t.Fatalf("tree.leavesValues incorrect: %s", err.Error())
}
if tree.nodes[0].Flags&categorical == 0 {
t.Fatal("first node should have categorical threshold")
}
if tree.nodes[0].Flags&catOneHot == 0 {
t.Fatal("first node should have one hot decision rule")
}
if tree.nodes[0].Flags&leftLeaf == 0 {
t.Fatal("first node should have right leaf")
}
if tree.nodes[0].Left != 0 {
t.Fatal("first node should have leaf index 0")
}
if tree.nodes[0].Flags&missingNan == 0 {
t.Fatal("first node should have missing nan")
}
if uint32(tree.nodes[0].Threshold) != 100 {
t.Fatal("first node should have threshold = 100")
}
if tree.nodes[1].Flags&categorical != 0 {
t.Fatal("second node should have numerical threshold")
}
if tree.nodes[1].Flags&defaultLeft == 0 {
t.Fatal("second node should have default left")
}
if tree.nodes[1].Flags&rightLeaf == 0 {
t.Fatal("second node should have left leaf")
}
if tree.nodes[1].Right != 2 {
t.Fatal("second node should have leaf index 2")
}
if tree.nodes[1].Flags&leftLeaf == 0 {
t.Fatal("second node should have right leaf")
}
if tree.nodes[1].Left != 1 {
t.Fatal("second node should have leaf index 1")
}
}
func TestLGTreeLeaf1(t *testing.T) {
path := filepath.Join("testdata", "tree_1leaf.txt")
reader, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
bufReader := bufio.NewReader(reader)
tree, err := lgTreeFromReader(bufReader)
if err != nil {
t.Fatal(err)
}
if tree.nLeaves() != 1 {
t.Fatalf("expected tree with 1 leaves (got %d)", tree.nLeaves())
}
if tree.nNodes() != 0 {
t.Fatalf("expected tree with 0 node (got %d)", tree.nNodes())
}
fvals := []float64{0.0}
check := func(truePred float64) {
p, _ := tree.predict(fvals)
if !util.AlmostEqualFloat64(p, truePred, 1e-3) {
t.Errorf("expected prediction %f, got %f", truePred, p)
}
}
check(0.123)
fvals[0] = 10.0
check(0.123)
fvals[0] = -10.0
check(0.123)
fvals[0] = math.NaN()
check(0.123)
}
func TestLGTreeLeaves2(t *testing.T) {
path := filepath.Join("testdata", "tree_2leaves.txt")
reader, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
bufReader := bufio.NewReader(reader)
tree, err := lgTreeFromReader(bufReader)
if err != nil {
t.Fatal(err)
}
if tree.nLeaves() != 2 {
t.Fatalf("expected tree with 2 leaves (got %d)", tree.nLeaves())
}
if tree.nNodes() != 1 {
t.Fatalf("expected tree with 1 node (got %d)", tree.nNodes())
}
fvals := []float64{0.0}
check := func(truePred float64) {
p, _ := tree.predict(fvals)
if !util.AlmostEqualFloat64(p, truePred, 1e-3) {
t.Errorf("expected prediction %f, got %f", truePred, p)
}
}
check(0.43)
fvals[0] = 5.1
check(0.59)
fvals[0] = math.NaN()
check(0.43)
}
func TestLGTreeLeaves3(t *testing.T) {
path := filepath.Join("testdata", "tree_3leaves.txt")
reader, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
bufReader := bufio.NewReader(reader)
tree, err := lgTreeFromReader(bufReader)
if err != nil {
t.Fatal(err)
}
if tree.nLeaves() != 3 {
t.Fatalf("expected tree with 3 leaves (got %d)", tree.nLeaves())
}
if tree.nNodes() != 2 {
t.Fatalf("expected tree with 2 node (got %d)", tree.nNodes())
}
fvals := []float64{0.0, 0.0}
check := func(truePred float64) {
p, _ := tree.predict(fvals)
if !util.AlmostEqualFloat64(p, truePred, 1e-3) {
t.Errorf("expected prediction %f, got %f", truePred, p)
}
}
check(0.35)
fvals[0] = 1000.0
check(0.38)
fvals[0] = math.NaN()
check(0.35)
fvals[1] = 10.0
check(0.35)
fvals[1] = 100.0
check(0.54)
}
func checkPredLeaves(t *testing.T, predicted []float64, trueIds *mat.DenseMat) {
rows := trueIds.Rows
cols := trueIds.Cols
if len(predicted) != rows*cols {
t.Fatalf("predeicted size mismatch")
}
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
if uint32(trueIds.Values[row*cols+col]) != uint32(predicted[row*cols+col]) {
t.Fatalf("Predicted leaves don't match %v, %v at row = %d, col = %d", predicted, trueIds.Values, row, col)
}
}
}
}
func TestLGPredLeaf(t *testing.T) {
modelPath := filepath.Join("testdata", "lg_breast_cancer.txt")
testPath := filepath.Join("testdata", "lg_breast_cancer_data.txt")
predLeavesTruthPath := filepath.Join("testdata", "lg_breast_cancer_data_pred_leaves.txt")
model, err := LGEnsembleFromFile(modelPath, false)
if err != nil {
t.Fatal(err)
}
model = model.EnsembleWithLeafPredictions()
test, err := mat.DenseMatFromCsvFile(testPath, 0, false, " ", 0.0)
predLeavesTruth, err := mat.DenseMatFromCsvFile(predLeavesTruthPath, 0, false, " ", 0.0)
// Test Single
fvals := test.Values[:test.Cols]
res := model.PredictSingle(fvals, 0)
if res != 0.0 {
t.Errorf("Failed PredictSingle should return 0.0")
}
// Test Single
predictions := make([]float64, 1*model.NOutputGroups())
err = model.Predict(fvals, 0, predictions)
if err != nil {
t.Fatal(err)
}
checkPredLeaves(t, predictions, &mat.DenseMat{Values: predLeavesTruth.Values[0:predLeavesTruth.Cols], Cols: predLeavesTruth.Cols, Rows: 1})
// Test Dense
predictionsDense := make([]float64, test.Rows*model.NOutputGroups())
err = model.PredictDense(test.Values, test.Rows, test.Cols, predictionsDense, 0, 0)
if err != nil {
t.Fatal(err)
}
checkPredLeaves(t, predictionsDense, predLeavesTruth)
// Test batch and multi thread
err = model.PredictDense(test.Values, test.Rows, test.Cols, predictionsDense, 0, 5)
if err != nil {
t.Fatal(err)
}
checkPredLeaves(t, predictionsDense, predLeavesTruth)
testCSR, err := mat.CSRMatFromArray(test.Values, test.Rows, test.Cols)
if err != nil {
t.Fatal(err)
}
// Test CSR
predictionsCSR := make([]float64, testCSR.Rows()*model.NOutputGroups())
err = model.PredictCSR(testCSR.RowHeaders, testCSR.ColIndexes, testCSR.Values, predictionsCSR, 0, 1)
if err != nil {
t.Fatal(err)
}
checkPredLeaves(t, predictionsCSR, predLeavesTruth)
// Test batch and multi thread
err = model.PredictCSR(testCSR.RowHeaders, testCSR.ColIndexes, testCSR.Values, predictionsCSR, 0, 5)
if err != nil {
t.Fatal(err)
}
checkPredLeaves(t, predictionsCSR, predLeavesTruth)
}
func TestLGEnsemble(t *testing.T) {
path := filepath.Join("testdata", "model_simple.txt")
model, err := LGEnsembleFromFile(path, false)
if err != nil {
t.Fatal(err)
}
if model.NEstimators() != 2 {
t.Fatalf("expected 2 trees (got %d)", model.NEstimators())
}
denseValues := []float64{0.0, 0.0,
1000.0, 0.0,
800.0, 0.0,
800.0, 100,
0.0, 100,
1000, math.NaN(),
math.NaN(), math.NaN(),
}
denseRows := 7
denseCols := 2
// check predictions
predictions := make([]float64, denseRows)
model.PredictDense(denseValues, denseRows, denseCols, predictions, 0, 0)
truePredictions := []float64{0.29462594, 0.39565483, 0.39565483, 0.69580371, 0.69580371, 0.39565483, 0.29462594}
if err := util.AlmostEqualFloat64Slices(predictions, truePredictions, 1e-7); err != nil {
t.Fatalf("predictions on dense not correct (all trees): %s", err.Error())
}
// check prediction only on first tree
model.PredictDense(denseValues, denseRows, denseCols, predictions, 1, 0)
truePredictions = []float64{0.35849878, 0.41213916, 0.41213916, 0.56697267, 0.56697267, 0.41213916, 0.35849878}
if err := util.AlmostEqualFloat64Slices(predictions, truePredictions, 1e-7); err != nil {
t.Fatalf("predictions on dense not correct (all trees): %s", err.Error())
}
}
func TestLGEnsembleJSON1tree1leaf(t *testing.T) {
modelPath := filepath.Join("testdata", "lg_1tree_1leaf.json")
// loading model
modelFile, err := os.Open(modelPath)
if err != nil {
t.Fatal(err)
}
defer modelFile.Close()
model, err := LGEnsembleFromJSON(modelFile, false)
if err != nil {
t.Fatal(err)
}
if model.NEstimators() != 1 {
t.Fatalf("expected 1 trees (got %d)", model.NEstimators())
}
if model.NOutputGroups() != 1 {
t.Fatalf("expected 1 class (got %d)", model.NOutputGroups())
}
if model.NFeatures() != 41 {
t.Fatalf("expected 41 class (got %d)", model.NFeatures())
}
features := make([]float64, model.NFeatures())
pred := model.PredictSingle(features, 0)
if pred != 0.42 {
t.Fatalf("expected prediction 0.42 (got %f)", pred)
}
}
func TestLGEnsembleJSON1tree(t *testing.T) {
modelPath := filepath.Join("testdata", "lg_1tree.json")
// loading model
modelFile, err := os.Open(modelPath)
if err != nil {
t.Fatal(err)
}
defer modelFile.Close()
model, err := LGEnsembleFromJSON(modelFile, false)
if err != nil {
t.Fatal(err)
}
if model.NEstimators() != 1 {
t.Fatalf("expected 1 trees (got %d)", model.NEstimators())
}
if model.NOutputGroups() != 1 {
t.Fatalf("expected 1 class (got %d)", model.NOutputGroups())
}
if model.NFeatures() != 2 {
t.Fatalf("expected 2 class (got %d)", model.NFeatures())
}
check := func(features []float64, trueAnswer float64) {
pred := model.PredictSingle(features, 0)
if pred != trueAnswer {
t.Fatalf("expected prediction %f (got %f)", trueAnswer, pred)
}
}
check([]float64{0.0, 0.0}, 0.4242)
check([]float64{0.0, 11.0}, 0.4242)
check([]float64{0.13, 11.0}, 0.4242)
check([]float64{0.0, 1.0}, 0.4703)
check([]float64{0.0, 10.0}, 0.4703)
check([]float64{0.0, 100.0}, 0.4703)
check([]float64{0.15, 0.0}, 1.1111)
check([]float64{0.15, 11.0}, 1.1111)
}