-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.go
59 lines (53 loc) · 1.51 KB
/
main3.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
package main
import (
"fmt"
_ "github.com/sjwhitworth/golearn"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/ensemble"
"github.com/sjwhitworth/golearn/evaluation"
"os"
)
func main() {
rawData, err := base.ParseCSVToInstances("db/out.csv", true)
if err != nil {
panic(err)
}
//fmt.Println(rawData)
//cls := knn.NewKnnClassifier("euclidean", "linear", 2)
cls := ensemble.NewRandomForest(10, 5)
trainData, valData := base.InstancesTrainTestSplit(rawData, 0.50)
err = cls.Fit(trainData)
if err != nil {
println("FIT ERROR", err.Error())
}
validations, err := cls.Predict(valData)
if err != nil {
println("TRAIN PREDICT ERROR", err.Error())
}
confusionMat, err := evaluation.GetConfusionMatrix(valData, validations)
if err != nil {
panic(fmt.Sprintf("Unable to get confusion matrix: %s", err.Error()))
}
fmt.Println(evaluation.GetSummary(confusionMat))
err = cls.Save("model.h")
if err != nil {
println("MODEL SAVE ERROR", err.Error())
}
testData, err := base.ParseCSVToInstances("db/out2.csv", true)
cls2 := ensemble.RandomForest{}
err = cls2.Load("model.h")
if err != nil {
println("COULD NOT LOAD MODEL", err.Error())
}
predictions, err := cls2.Predict(testData)
if err != nil {
println("TEST PREDICT ERROR", err.Error())
}
println(predictions.Size())
writer, _ := os.OpenFile("db/out3.csv", os.O_CREATE|os.O_WRONLY, os.ModePerm)
err = base.SerializeInstancesToCSVStream(predictions, writer)
if err != nil {
println("CSV SAVE ERROR", err.Error())
}
writer.Close()
}