-
Notifications
You must be signed in to change notification settings - Fork 108
/
algo_runner.go
204 lines (161 loc) · 5.39 KB
/
algo_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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
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"
"github.com/xlvector/hector/eval"
"os"
"strconv"
)
func AlgorithmRun(classifier algo.Classifier,
train_path string, test_path string, pred_path string,
params map[string]string) (float64, []*eval.LabelPrediction, 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, nil, err
}
test_dataset := core.NewDataSet()
err = test_dataset.Load(test_path, global)
if err != nil {
return 0.5, nil, err
}
classifier.Init(params)
auc, predictions := AlgorithmRunOnDataSet(classifier, train_dataset,
test_dataset, pred_path, params)
return auc, predictions, nil
}
func AlgorithmTrain(classifier algo.Classifier, 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 AlgorithmTest(classifier algo.Classifier, test_path string, pred_path string, params map[string]string) (float64, []*eval.LabelPrediction, 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, nil
}
test_dataset := core.NewDataSet()
err := test_dataset.Load(test_path, global)
if err != nil {
return 0.0, nil, err
}
auc, predictions := AlgorithmRunOnDataSet(classifier, nil, test_dataset, pred_path, params)
return auc, predictions, nil
}
func AlgorithmRunOnDataSet(classifier algo.Classifier, train_dataset, test_dataset *core.DataSet, pred_path string, params map[string]string) (float64, []*eval.LabelPrediction) {
if train_dataset != nil {
classifier.Train(train_dataset)
}
predictions := []*eval.LabelPrediction{}
var pred_file *os.File
if pred_path != "" {
pred_file, _ = os.Create(pred_path)
}
for _, sample := range test_dataset.Samples {
prediction := classifier.Predict(sample)
if pred_file != nil {
pred_file.WriteString(strconv.FormatFloat(prediction, 'g', 5, 64) + "\n")
}
predictions = append(predictions, &(eval.LabelPrediction{Label: sample.Label, Prediction: prediction}))
}
if pred_path != "" {
defer pred_file.Close()
}
auc := eval.AUC(predictions)
return auc, predictions
}
/* Regression */
func RegAlgorithmRun(regressor algo.Regressor, train_path string, test_path string, pred_path string, params map[string]string) (float64, []*eval.RealPrediction, error) {
global, _ := strconv.ParseInt(params["global"], 10, 64)
train_dataset := core.NewRealDataSet()
err := train_dataset.Load(train_path, global)
if err != nil {
return 0.5, nil, err
}
test_dataset := core.NewRealDataSet()
err = test_dataset.Load(test_path, global)
if err != nil {
return 0.5, nil, err
}
regressor.Init(params)
rmse, predictions := RegAlgorithmRunOnDataSet(regressor, train_dataset, test_dataset, pred_path, params)
return rmse, predictions, nil
}
func RegAlgorithmTrain(regressor algo.Regressor, train_path string, params map[string]string) error {
global, _ := strconv.ParseInt(params["global"], 10, 64)
train_dataset := core.NewRealDataSet()
err := train_dataset.Load(train_path, global)
if err != nil {
return err
}
regressor.Init(params)
regressor.Train(train_dataset)
model_path, _ := params["model"]
if model_path != "" {
regressor.SaveModel(model_path)
}
return nil
}
func RegAlgorithmTest(regressor algo.Regressor, test_path string, pred_path string, params map[string]string) (float64, []*eval.RealPrediction, error) {
global, _ := strconv.ParseInt(params["global"], 10, 64)
model_path, _ := params["model"]
regressor.Init(params)
if model_path != "" {
regressor.LoadModel(model_path)
} else {
return 0.0, nil, nil
}
test_dataset := core.NewRealDataSet()
err := test_dataset.Load(test_path, global)
if err != nil {
return 0.0, nil, err
}
rmse, predictions := RegAlgorithmRunOnDataSet(regressor, nil, test_dataset, pred_path, params)
return rmse, predictions, nil
}
func RegAlgorithmRunOnDataSet(regressor algo.Regressor, train_dataset, test_dataset *core.RealDataSet, pred_path string, params map[string]string) (float64, []*eval.RealPrediction) {
if train_dataset != nil {
regressor.Train(train_dataset)
}
predictions := []*eval.RealPrediction{}
var pred_file *os.File
if pred_path != "" {
pred_file, _ = os.Create(pred_path)
}
for _, sample := range test_dataset.Samples {
prediction := regressor.Predict(sample)
if pred_file != nil {
pred_file.WriteString(strconv.FormatFloat(prediction, 'g', 5, 64) + "\n")
}
predictions = append(predictions, &eval.RealPrediction{Value: sample.Value, Prediction: prediction})
}
if pred_path != "" {
defer pred_file.Close()
}
rmse := eval.RegRMSE(predictions)
return rmse, predictions
}