forked from lazywei/lineargo
/
liblinear.go
200 lines (173 loc) · 4.82 KB
/
liblinear.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
package liblinear
/*
#cgo LDFLAGS: -llinear
#include <linear.h>
#include <stdio.h>
*/
import "C"
import (
"errors"
"fmt"
"github.com/gonum/matrix/mat64"
)
type FeatureNode struct {
// struct feature_node
// {
// int index;
// double value;
// };
cFeatureNode *C.struct_feature_node
}
func NewFeatureNode(index int, value float64) *FeatureNode {
return &FeatureNode{
cFeatureNode: &C.struct_feature_node{
index: C.int(index),
value: C.double(value),
},
}
}
func (fn *FeatureNode) GetData() (int, float64) {
return int(fn.cFeatureNode.index), float64(fn.cFeatureNode.value)
}
func (fn *FeatureNode) GetPtr() *C.struct_feature_node {
return fn.cFeatureNode
}
type Model struct {
// struct model
// {
// struct parameter param;
// int nr_class; /* number of classes */
// int nr_feature;
// double *w;
// int *label; /* label of each class */
// double bias;
// };
cModel *C.struct_model
}
func toFeatureNodes(X *mat64.Dense) []*C.struct_feature_node {
featureNodes := []*C.struct_feature_node{}
nRows, nCols := X.Dims()
for i := 0; i < nRows; i++ {
row := []C.struct_feature_node{}
for j := 0; j < nCols; j++ {
val := X.At(i, j)
if val != 0 {
row = append(row, C.struct_feature_node{
index: C.int(j + 1),
value: C.double(val),
})
}
}
row = append(row, C.struct_feature_node{
index: C.int(-1),
value: C.double(0),
})
featureNodes = append(featureNodes, &row[0])
}
return featureNodes
}
// Wrapper for the `train` function in liblinear.
//
// `model* train(const struct problem *prob, const struct parameter *param);`
//
// The explanation of parameters are:
//
// solverType:
//
// for multi-class classification
// 0 -- L2-regularized logistic regression (primal)
// 1 -- L2-regularized L2-loss support vector classification (dual)
// 2 -- L2-regularized L2-loss support vector classification (primal)
// 3 -- L2-regularized L1-loss support vector classification (dual)
// 4 -- support vector classification by Crammer and Singer
// 5 -- L1-regularized L2-loss support vector classification
// 6 -- L1-regularized logistic regression
// 7 -- L2-regularized logistic regression (dual)
// for regression
// 11 -- L2-regularized L2-loss support vector regression (primal)
// 12 -- L2-regularized L2-loss support vector regression (dual)
// 13 -- L2-regularized L1-loss support vector regression (dual)
//
// eps is the stopping criterion.
//
// C_ is the cost of constraints violation.
//
// p is the sensitiveness of loss of support vector regression.
//
// classWeights is a map from int to float64, with the key be the class and the
// value be the weight. For example, {1: 10, -1: 0.5} means giving weight=10 for
// class=1 while weight=0.5 for class=-1
//
// If you do not want to change penalty for any of the classes, just set
// classWeights to nil.
func Train(X, y *mat64.Dense, bias float64, pm *Parameter) *Model {
var problem C.struct_problem
nRows, nCols := X.Dims()
cY := mapCDouble(y.Col(nil, 0))
cX := toFeatureNodes(X)
problem.x = &cX[0]
problem.y = &cY[0]
problem.n = C.int(nCols)
problem.l = C.int(nRows)
problem.bias = C.double(bias)
model := C.train(&problem, pm.GetPtr())
return &Model{
cModel: model,
}
}
// double predict(const struct model *model_, const struct feature_node *x);
func Predict(model *Model, X *mat64.Dense) *mat64.Dense {
nRows, _ := X.Dims()
cXs := toFeatureNodes(X)
y := mat64.NewDense(nRows, 1, nil)
for i, cX := range cXs {
y.Set(i, 0, float64(C.predict(model.cModel, cX)))
}
return y
}
// double predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates);
func PredictProba(model *Model, X *mat64.Dense) *mat64.Dense {
nRows, _ := X.Dims()
nrClasses := int(C.get_nr_class(model.cModel))
cXs := toFeatureNodes(X)
y := mat64.NewDense(nRows, nrClasses, nil)
proba := make([]C.double, nrClasses, nrClasses)
for i, cX := range cXs {
C.predict_probability(model.cModel, cX, &proba[0])
for j := 0; j < nrClasses; j++ {
y.Set(i, j, float64(proba[j]))
}
}
return y
}
func Accuracy(y_true, y_pred *mat64.Dense) float64 {
y1 := y_true.Col(nil, 0)
y2 := y_pred.Col(nil, 0)
total := 0.0
correct := 0.0
for i := 0; i < len(y1); i++ {
if y1[i] == y2[i] {
correct++
}
total++
}
return correct / total
}
func SaveModel(model *Model, filename string) {
rtn := C.save_model(C.CString(filename), model.cModel)
if int(rtn) != 0 {
errStr := fmt.Sprintf("Error Code `%v` when trying to save model", int(rtn))
fmt.Println(errStr)
panic(errors.New(errStr))
}
}
func LoadModel(filename string) *Model {
model := C.load_model(C.CString(filename))
if model == nil {
errStr := fmt.Sprintf("Can't load model from %v", filename)
panic(errors.New(errStr))
}
return &Model{
cModel: model,
}
}