-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.go
62 lines (56 loc) · 1.26 KB
/
predict.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
package xgb
import (
"go4ml.xyz/base/tables"
"go4ml.xyz/iokit"
"go4ml.xyz/xgb/capi"
"io"
"io/ioutil"
"runtime"
)
type predictionModel struct {
features []string
predicts string
source iokit.Input
}
func (x *xgbinstance) MapFeatures(t *tables.Table) (*tables.Table, error) {
matrix, err := t.Matrix(x.features)
if err != nil {
panic(err)
}
m := matrix32(matrix)
defer m.Free()
y := capi.Predict(x.handle, m.handle, 0)
pred := tables.Matrix{
Features: y,
Labels: nil,
Width: len(y) / matrix.Length,
Length: matrix.Length,
LabelsWidth: 0,
}
return t.Except(x.features...).With(pred.AsColumn(), x.predicts), nil
}
func (model predictionModel) Features() []string {
return model.features
}
func (model predictionModel) Predicted() string {
return model.predicts
}
func (model predictionModel) FeaturesMapper(int) (fm tables.FeaturesMapper, err error) {
var rd io.ReadCloser
if rd, err = model.source.Open(); err != nil {
return
}
defer rd.Close()
var bs []byte
if bs, err = ioutil.ReadAll(rd); err != nil {
return
}
x := &xgbinstance{
handle: capi.Create(),
features: model.features,
predicts: model.predicts,
}
runtime.SetFinalizer(x, func(x *xgbinstance) { x.Close() })
capi.SetModel(x.handle, bs)
return x, nil
}