2014-05-14 14:00:22 +01:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2016-06-14 00:56:47 +01:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2014-08-22 07:21:24 +00:00
|
|
|
"github.com/sjwhitworth/golearn/base"
|
2014-08-22 09:33:42 +00:00
|
|
|
"github.com/sjwhitworth/golearn/evaluation"
|
2014-08-22 07:21:24 +00:00
|
|
|
"github.com/sjwhitworth/golearn/filters"
|
|
|
|
"github.com/sjwhitworth/golearn/trees"
|
2014-08-22 13:16:11 +00:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2014-05-14 14:00:22 +01:00
|
|
|
)
|
|
|
|
|
2014-08-22 08:13:19 +00:00
|
|
|
func BenchmarkBaggingRandomForestFit(t *testing.B) {
|
2014-05-23 11:56:23 +01:00
|
|
|
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
|
|
|
|
if err != nil {
|
2016-06-14 00:56:47 +01:00
|
|
|
t.Fatalf("Unable to parse CSV to instances: %s", err.Error())
|
2014-05-23 11:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
filt := filters.NewChiMergeFilter(inst, 0.90)
|
2014-08-02 16:22:15 +01:00
|
|
|
for _, a := range base.NonClassFloatAttributes(inst) {
|
|
|
|
filt.AddAttribute(a)
|
|
|
|
}
|
|
|
|
filt.Train()
|
|
|
|
instf := base.NewLazilyFilteredInstances(inst, filt)
|
2014-08-22 07:58:01 +00:00
|
|
|
|
2014-05-23 11:56:23 +01:00
|
|
|
rf := new(BaggedModel)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
rf.AddModel(trees.NewRandomTree(2))
|
|
|
|
}
|
2014-08-22 07:58:01 +00:00
|
|
|
|
2014-08-22 08:13:19 +00:00
|
|
|
t.ResetTimer()
|
2014-05-23 11:56:23 +01:00
|
|
|
for i := 0; i < 20; i++ {
|
2014-08-02 16:22:15 +01:00
|
|
|
rf.Fit(instf)
|
2014-05-23 11:56:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:13:19 +00:00
|
|
|
func BenchmarkBaggingRandomForestPredict(t *testing.B) {
|
2014-05-23 11:56:23 +01:00
|
|
|
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
|
|
|
|
if err != nil {
|
2016-06-14 00:56:47 +01:00
|
|
|
t.Fatalf("Unable to parse CSV to instances: %s", err.Error())
|
2014-05-23 11:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
filt := filters.NewChiMergeFilter(inst, 0.90)
|
2014-08-02 16:22:15 +01:00
|
|
|
for _, a := range base.NonClassFloatAttributes(inst) {
|
|
|
|
filt.AddAttribute(a)
|
|
|
|
}
|
|
|
|
filt.Train()
|
|
|
|
instf := base.NewLazilyFilteredInstances(inst, filt)
|
2014-08-22 07:58:01 +00:00
|
|
|
|
2014-05-23 11:56:23 +01:00
|
|
|
rf := new(BaggedModel)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
rf.AddModel(trees.NewRandomTree(2))
|
|
|
|
}
|
2014-08-22 07:58:01 +00:00
|
|
|
|
2014-08-02 16:22:15 +01:00
|
|
|
rf.Fit(instf)
|
2014-08-22 08:13:19 +00:00
|
|
|
t.ResetTimer()
|
2014-05-23 11:56:23 +01:00
|
|
|
for i := 0; i < 20; i++ {
|
2014-08-02 16:22:15 +01:00
|
|
|
rf.Predict(instf)
|
2014-05-23 11:56:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:16:11 +00:00
|
|
|
func TestBaggedModelRandomForest(t *testing.T) {
|
|
|
|
Convey("Given data", t, func() {
|
|
|
|
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
|
|
|
|
So(err, ShouldBeNil)
|
2014-05-14 14:00:22 +01:00
|
|
|
|
2014-08-22 13:16:11 +00:00
|
|
|
Convey("Splitting the data into training and test data", func() {
|
|
|
|
trainData, testData := base.InstancesTrainTestSplit(inst, 0.6)
|
2014-08-22 07:58:01 +00:00
|
|
|
|
2014-08-22 13:16:11 +00:00
|
|
|
Convey("Filtering the split datasets", func() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
filt := filters.NewChiMergeFilter(inst, 0.90)
|
|
|
|
for _, a := range base.NonClassFloatAttributes(inst) {
|
|
|
|
filt.AddAttribute(a)
|
|
|
|
}
|
|
|
|
filt.Train()
|
|
|
|
trainDataf := base.NewLazilyFilteredInstances(trainData, filt)
|
|
|
|
testDataf := base.NewLazilyFilteredInstances(testData, filt)
|
2014-08-22 07:58:01 +00:00
|
|
|
|
2014-08-22 13:16:11 +00:00
|
|
|
Convey("Fitting and Predicting with a Bagged Model of 10 Random Trees", func() {
|
|
|
|
rf := new(BaggedModel)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
rf.AddModel(trees.NewRandomTree(2))
|
|
|
|
}
|
|
|
|
|
|
|
|
rf.Fit(trainDataf)
|
|
|
|
predictions := rf.Predict(testDataf)
|
|
|
|
|
|
|
|
confusionMat, err := evaluation.GetConfusionMatrix(testDataf, predictions)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Predictions are somewhat accurate", func() {
|
|
|
|
So(evaluation.GetAccuracy(confusionMat), ShouldBeGreaterThan, 0.5)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-05-14 14:00:22 +01:00
|
|
|
}
|