forked from NickolayNesterenko/catboost-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
classifier.go
38 lines (32 loc) · 1.02 KB
/
classifier.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
package catboost
import "math"
// BinaryClassifer is wrapper over model object that add methods for binary classification
type BinaryClassifer struct {
Model *Model
}
func sigmoid(probit float64) float64 {
return 1.0 / (1.0 + math.Exp(-probit))
}
// LoadBinaryClassifierFromFile loads binary classifier from file
func LoadBinaryClassifierFromFile(filename string) (*BinaryClassifer, error) {
model, err := LoadFullModelFromFile(filename)
if err != nil {
return nil, err
}
return &BinaryClassifer{Model: model}, nil
}
// PredictProba returns sigmoid scores which could be interpreted like probability
func (bc *BinaryClassifer) PredictProba(floats [][]float32, floatLength int, cats [][]string, catLength int) ([]float64, error) {
results, err := bc.Model.CalcModelPrediction(floats, floatLength, cats, catLength)
if err != nil {
return nil, err
}
for i := range results {
results[i] = sigmoid(results[i])
}
return results, nil
}
// Close deletes model handler
func (bc *BinaryClassifer) Close() {
bc.Model.Close()
}