-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional_test.go
81 lines (63 loc) · 2.46 KB
/
functional_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
package rf
import (
"rf/algo"
"rf/algo/decision"
"rf/algo/ensemble"
"rf/eval"
"rf/io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tobgu/qframe/config/csv"
)
func TestFunctional_DecisionTree(t *testing.T) {
types := map[string]string{"y": "float"}
var model algo.Model
df := io.LoadCsv("./testdata/data_banknote_authentication.txt", csv.Headers([]string{"col_0", "col_1", "col_2", "col_3", "y"}), csv.Types(types))
m := io.ToMatrix(df)
model = decision.Fit(m, -1, map[string]int{"maxDepth": 5, "minSize": 10})
preds := model.Predict(m)
y, _ := df.FloatView("y")
a := eval.Accuracy(y.Slice(), preds)
t.Log(model)
t.Log(a)
assert.Greater(t, a, 97.0)
}
func BenchmarkFit_DecisionTree(b *testing.B) {
for i := 0; i < b.N; i++ {
types := map[string]string{"y": "float"}
df := io.LoadCsv("./testdata/data_banknote_authentication.txt", csv.Headers([]string{"col_0", "col_1", "col_2", "col_3", "y"}), csv.Types(types))
m := io.ToMatrix(df)
dt := decision.Fit(m, -1, map[string]int{"maxDepth": 5, "minSize": 10})
b.Log(dt)
}
}
func TestFunctional_RandomForest(t *testing.T) {
types := map[string]string{"y": "float"}
df := io.LoadCsv("./testdata/data_banknote_authentication.txt", csv.Headers([]string{"col_0", "col_1", "col_2", "col_3", "y"}), csv.Types(types))
m := io.ToMatrix(df)
model := ensemble.Fit(m, -1, map[string]int{"n_estimator": 5, "maxDepth": 5, "minSize": 10})
preds := model.Predict(m)
y, _ := df.FloatView("y")
a := eval.Accuracy(y.Slice(), preds)
t.Log(model)
t.Log("Accuracy", a)
assert.Greater(t, a, 90.0)
}
func BenchmarkFit_RandomForest(b *testing.B) {
for i := 0; i < b.N; i++ {
types := map[string]string{"y": "float"}
df := io.LoadCsv("./testdata/data_banknote_authentication.txt", csv.Headers([]string{"col_0", "col_1", "col_2", "col_3", "y"}), csv.Types(types))
m := io.ToMatrix(df)
rf := ensemble.Fit(m, -1, map[string]int{"n_estimator": 5, "maxDepth": 5, "minSize": 10})
b.Log(rf)
}
}
func TestAlgorythms_Compare_Accuracy(t *testing.T) {
types := map[string]string{"y": "float"}
df := io.LoadCsv("./testdata/data_banknote_authentication.txt", csv.Headers([]string{"col_0", "col_1", "col_2", "col_3", "y"}), csv.Types(types))
m := io.ToMatrix(df)
scores := eval.CrossVal(m, 4, 5, decision.Fit, map[string]int{"maxDepth": 5, "minSize": 10})
t.Log("Decision Tree", scores)
scores = eval.CrossVal(m, 4, 5, ensemble.Fit, map[string]int{"n_estimator": 5, "maxDepth": 5, "minSize": 10})
t.Log("RandoForest", scores)
}