forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.go
121 lines (93 loc) · 2.72 KB
/
features.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
package dlframework
import (
"math"
"sort"
"github.com/pkg/errors"
)
type Features []*Feature
// Len is the number of elements in the collection.
func (p Features) Len() int {
return len(p)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (p Features) Less(i, j int) bool {
pi := p[i].Probability
pj := p[j].Probability
return !(pi < pj || math.IsNaN(float64(pi)) && !math.IsNaN(float64(pj)))
}
// Swap swaps the elements with indexes i and j.
func (p Features) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p Features) Sort() {
sort.Sort(p)
}
func (p Features) Take(n int) Features {
if p.Len() <= n {
return p
}
return Features(p[:n])
}
func (p Features) ProbabilitiesFloat32() []float32 {
pProbs := make([]float32, p.Len())
for ii := 0; ii < p.Len(); ii++ {
pProbs[ii] = p[ii].Probability
}
return pProbs
}
func (p Features) ProbabilitiesFloat64() []float64 {
pProbs := make([]float64, p.Len())
for ii := 0; ii < p.Len(); ii++ {
pProbs[ii] = float64(p[ii].Probability)
}
return pProbs
}
func (p Features) KullbackLeiblerDivergence(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return KullbackLeibler(pProbs, qProbs), nil
}
func (p Features) Correlation(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Correlation(pProbs, qProbs, nil), nil
}
func (p Features) Covariance(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Covariance(pProbs, qProbs, nil), nil
}
func (p Features) JensenShannon(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return JensenShannon(pProbs, qProbs), nil
}
func (p Features) Bhattacharyya(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Bhattacharyya(pProbs, qProbs), nil
}
func (p Features) Hellinger(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Hellinger(pProbs, qProbs), nil
}