forked from phuongdo/catboost-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassifier.go
47 lines (41 loc) · 1.22 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
39
40
41
42
43
44
45
46
47
package catboost
import "math"
// BinaryClassifier is wrapper over model object that add methods for binary classification
type BinaryClassifier 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) (*BinaryClassifier, error) {
model, err := LoadFullModelFromFile(filename)
if err != nil {
return nil, err
}
return &BinaryClassifier{Model: model}, nil
}
// PredictProba returns sigmoid scores which could be interpreted like probability
func (bc *BinaryClassifier) PredictProba(floats [][]float32, floatLength int,
cats [][]string, catLength int,
text [][]string, textLength int,
embeddings [][][]float32, embeddingDimensions []int, embeddingSize int,
) ([]float64, error) {
results, err := bc.Model.CalcModelPredictionTextAndEmbeddings(
floats, floatLength,
cats, catLength,
text, textLength,
embeddings, embeddingDimensions, embeddingSize,
)
if err != nil {
return nil, err
}
for i := range results {
results[i] = sigmoid(results[i])
}
return results, nil
}
// Close deletes model handler
func (bc *BinaryClassifier) Close() {
bc.Model.Close()
}