forked from ryanbressler/CloudForest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
forest_test.go
164 lines (138 loc) · 3.54 KB
/
forest_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
package CloudForest
import (
"encoding/csv"
"fmt"
"os"
"strconv"
"strings"
"testing"
"github.com/bmizerany/assert"
)
var (
predFilePath = "preds.csv"
inBagFilePath = "n.csv"
)
func TestPartialDependencyCategorical(t *testing.T) {
irisreader := strings.NewReader(irislibsvm)
fm := ParseLibSVM(irisreader)
tgt := fm.Data[1]
model := GrowRandomForest(fm, tgt, &ForestConfig{
NSamples: fm.Data[1].Length(),
MTry: 3,
NTrees: 500,
LeafSize: 1,
})
forest := model.Forest
// Partial Dependency Plot with 1 variable
pdp, err := PDP(forest.Predict, fm, "0")
assert.Equal(t, nil, err)
assert.Equal(t, 3, len(pdp))
// ensure all the probabilities are unique
uniq := make(map[float64]struct{})
for _, x := range pdp {
assert.Equal(t, 2, len(x))
uniq[x[1]] = struct{}{}
}
assert.Equal(t, 3, len(uniq))
}
func TestPartialDependencyNumeric(t *testing.T) {
irisreader := strings.NewReader(irislibsvm)
fm := ParseLibSVM(irisreader)
// write dataset to CSV for R comparison/validation
if os.Getenv("WRITEDATA") != "" {
iris, err := os.Create("iris.csv")
assert.Equal(t, nil, err)
for _, feature := range fm.Data {
str := make([]string, feature.Length())
for i := 0; i < feature.Length(); i++ {
str[i] = feature.GetStr(i)
}
iris.WriteString(strings.Join(str, ","))
iris.Write([]byte("\n"))
}
err = iris.Close()
assert.Equal(t, nil, err)
}
tgt := fm.Data[0]
model := GrowRandomForest(fm, tgt, &ForestConfig{
NSamples: fm.Data[0].Length(),
MTry: 3,
NTrees: 500,
LeafSize: 1,
})
forest := model.Forest
// Partial Dependency Plot with 1 variable
single, err := PDP(forest.Predict, fm, "3")
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, single)
// Partial Dependency Plot with 2 variables
double, err := PDP(forest.Predict, fm, "3", "2")
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, double)
if os.Getenv("WRITEDATA") != "" {
writeDeps("singleDep.csv", single)
writeDeps("doubleDep.csv", double)
}
}
func writeDeps(name string, vals [][]float64) {
file, _ := os.Create(name)
for _, val := range vals {
writeSlice(file, val)
}
}
func writeSlice(f *os.File, vals []float64) {
str := make([]string, len(vals))
for i, v := range vals {
str[i] = strconv.FormatFloat(v, 'f', -1, 64)
}
f.WriteString(strings.Join(str, ","))
f.Write([]byte("\n"))
}
func TestJackKnife(t *testing.T) {
// read data
preds := readCsv(t, predFilePath)
inbag := readCsv(t, inBagFilePath)
// run jackknife
predictions, err := JackKnife(preds, inbag)
if err != nil {
t.Fatalf("error jack-knifing: %v", err)
}
if os.Getenv("EXPORT_JACKKNIFE") != "" {
file, err := os.Create("validation.csv")
if err != nil {
t.Fatalf("error creating file: %v", err)
}
defer file.Close()
fmt.Fprintln(file, "prediction, variance")
for _, pred := range predictions {
fmt.Fprintf(file, "%v, %v\n", pred.Value, pred.Variance)
}
}
}
func readCsv(t *testing.T, file string) [][]float64 {
predFile, err := os.Open(file)
if err != nil {
t.Fatalf("could not open file %v: %v", predFile, err)
}
reader := csv.NewReader(predFile)
all, err := reader.ReadAll()
if err != nil {
t.Fatalf("could not read file %s: %v", file, err)
}
values := make([][]float64, len(all))
for i, v := range all {
values[i] = strToFloat(t, v)
}
return values
}
func strToFloat(t *testing.T, values []string) []float64 {
f := make([]float64, len(values))
var err error
for i := range f {
f[i], err = strconv.ParseFloat(values[i], 64)
if err != nil {
t.Fatalf("could not convert %s, %v", values[i], err)
}
}
return f
}