-
Notifications
You must be signed in to change notification settings - Fork 108
/
mc_runner.go
111 lines (89 loc) · 2.75 KB
/
mc_runner.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
/*
Package hector is a golang based machine learning lib. It intend to implement all famous machine learning algoirhtms by golang.
Currently, it only support algorithms which can solve binary classification problems. Supported algorithms include:
1. Decision Tree (CART, Random Forest, GBDT)
2. Logistic Regression
3. SVM
4. Neural Network
*/
package hector
import (
"github.com/xlvector/hector/algo"
"github.com/xlvector/hector/core"
"os"
"strconv"
)
func MultiClassRun(classifier algo.MultiClassClassifier, train_path string, test_path string, pred_path string, params map[string]string) (float64, error) {
global, _ := strconv.ParseInt(params["global"], 10, 64)
train_dataset := core.NewDataSet()
err := train_dataset.Load(train_path, global)
if err != nil {
return 0.5, err
}
test_dataset := core.NewDataSet()
err = test_dataset.Load(test_path, global)
if err != nil {
return 0.5, err
}
classifier.Init(params)
accuracy := MultiClassRunOnDataSet(classifier, train_dataset, test_dataset, pred_path, params)
return accuracy, nil
}
func MultiClassTrain(classifier algo.MultiClassClassifier, train_path string, params map[string]string) error {
global, _ := strconv.ParseInt(params["global"], 10, 64)
train_dataset := core.NewDataSet()
err := train_dataset.Load(train_path, global)
if err != nil {
return err
}
classifier.Init(params)
classifier.Train(train_dataset)
model_path, _ := params["model"]
if model_path != "" {
classifier.SaveModel(model_path)
}
return nil
}
func MultiClassTest(classifier algo.MultiClassClassifier, test_path string, pred_path string, params map[string]string) (float64, error) {
global, _ := strconv.ParseInt(params["global"], 10, 64)
model_path, _ := params["model"]
classifier.Init(params)
if model_path != "" {
classifier.LoadModel(model_path)
} else {
return 0.0, nil
}
test_dataset := core.NewDataSet()
err := test_dataset.Load(test_path, global)
if err != nil {
return 0.0, err
}
accuracy := MultiClassRunOnDataSet(classifier, nil, test_dataset, pred_path, params)
return accuracy, nil
}
func MultiClassRunOnDataSet(classifier algo.MultiClassClassifier, train_dataset, test_dataset *core.DataSet, pred_path string, params map[string]string) float64 {
if train_dataset != nil {
classifier.Train(train_dataset)
}
var pred_file *os.File
if pred_path != "" {
pred_file, _ = os.Create(pred_path)
}
accuracy := 0.0
total := 0.0
for _, sample := range test_dataset.Samples {
prediction := classifier.PredictMultiClass(sample)
label, _ := prediction.KeyWithMaxValue()
if int(label) == sample.Label {
accuracy += 1.0
}
total += 1.0
if pred_file != nil {
pred_file.WriteString(strconv.Itoa(label) + "\n")
}
}
if pred_path != "" {
defer pred_file.Close()
}
return accuracy / total
}