forked from sjwhitworth/golearn
/
rf.go
40 lines (31 loc) · 925 Bytes
/
rf.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
// Demonstrates decision tree classification
package main
import (
"fmt"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/ensemble"
"github.com/sjwhitworth/golearn/evaluation"
"math"
"math/rand"
)
func main() {
var tree base.Classifier
// Load in the iris dataset
iris, err := base.ParseCSVToInstances("../datasets/iris_headers.csv", true)
if err != nil {
panic(err)
}
for i := 1; i < 60; i += 2 {
// Demonstrate the effect of adding more trees to the forest
// and also how much better it is without discretisation.
rand.Seed(44111342)
tree = ensemble.NewRandomForest(i, 4)
cfs, err := evaluation.GenerateCrossFoldValidationConfusionMatrices(iris, tree, 5)
if err != nil {
panic(err)
}
mean, variance := evaluation.GetCrossValidatedMetric(cfs, evaluation.GetAccuracy)
stdev := math.Sqrt(variance)
fmt.Printf("%d\t%.2f\t(+/- %.2f)\n", i, mean, stdev*2)
}
}