forked from schollz/find3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb.go
223 lines (204 loc) · 4.98 KB
/
nb.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package nb1
import (
"errors"
"math"
"sort"
"github.com/Nimaapr/find3/server/main/src/database"
"github.com/Nimaapr/find3/server/main/src/models"
)
// Algorithm defines the basic structure
type Algorithm struct {
Data map[string]map[string]map[int]int
isLoaded bool
}
// New returns new algorithm
func New() *Algorithm {
n := new(Algorithm)
n.Data = make(map[string]map[string]map[int]int)
n.isLoaded = false
return n
}
// Fit will take the data and learn it
func (a *Algorithm) Fit(datas []models.SensorData) (err error) {
if len(datas) == 0 {
err = errors.New("no data")
return
}
a.Data = make(map[string]map[string]map[int]int)
for _, data := range datas {
if _, ok := a.Data[data.Location]; !ok {
a.Data[data.Location] = make(map[string]map[int]int)
}
for sensorType := range data.Sensors {
for sensor := range data.Sensors[sensorType] {
mac := sensorType + "-" + sensor
val := int(data.Sensors[sensorType][sensor].(float64))
if _, ok := a.Data[data.Location][mac]; !ok {
a.Data[data.Location][mac] = make(map[int]int)
}
if _, ok := a.Data[data.Location][mac][val]; !ok {
a.Data[data.Location][mac][val] = 0
}
a.Data[data.Location][mac][val]++
}
}
}
db, err := database.Open(datas[0].Family)
if err != nil {
return
}
defer db.Close()
err = db.Set("NB1", a.Data)
return
}
// Classify will classify the specified data
func (a *Algorithm) Classify(data models.SensorData) (pl PairList, err error) {
// load data if not already
if !a.isLoaded {
db, err2 := database.Open(data.Family, true)
if err2 != nil {
err = err2
return
}
err = db.Get("NB1", &a.Data)
db.Close()
if err != nil {
return
}
a.isLoaded = true
}
if len(a.Data) == 0 {
err = errors.New("need to fit first")
return
}
numLocations := float64(len(a.Data))
NA := 1 / numLocations
NnotA := 1 - NA
Ps := make(map[string][]float64)
for location := range a.Data {
Ps[location] = []float64{}
}
for sensorType := range data.Sensors {
for name := range data.Sensors[sensorType] {
mac := sensorType + "-" + name
val := int(data.Sensors[sensorType][name].(float64))
for location := range Ps {
PA := a.probMacGivenLocation(mac, val, location, true)
PnotA := a.probMacGivenLocation(mac, val, location, false)
P := PA * NA / (PA*NA + PnotA*NnotA)
Ps[location] = append(Ps[location], math.Log(P))
}
}
}
PsumTotal := float64(0)
Psum := make(map[string]float64)
for location := range Ps {
Psum[location] = float64(0)
for _, v := range Ps[location] {
Psum[location] += v
}
Psum[location] = math.Exp(Psum[location])
PsumTotal += Psum[location]
}
for location := range Psum {
Psum[location] = Psum[location] / PsumTotal
}
pl = make(PairList, len(Psum))
i := 0
for k, v := range Psum {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return
}
type Pair struct {
Key string
Value float64
}
type PairList []Pair
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (a *Algorithm) probMacGivenLocation(mac string, val int, loc string, positive bool) (P float64) {
P = 0.005
valToCount := make(map[int]int)
newValToCount := make(map[int]int)
// positive: find val,count where loc = X and mac = X
// not positive: find val,count where loc != X and mac = X
for locX := range a.Data {
if positive {
if locX != loc {
continue
}
} else {
if locX == loc {
continue
}
}
for macX := range a.Data[locX] {
if macX != mac {
continue
}
for valX := range a.Data[locX][macX] {
valToCount[valX] = a.Data[locX][macX][valX]
newValToCount[valX] = a.Data[locX][macX][valX]
}
}
}
// apply gaussian filter
width := 3
gaussRange := []int{}
widthCubed := int(math.Pow(float64(width), 3))
for i := -1 * widthCubed; i <= widthCubed; i++ {
gaussRange = append(gaussRange, i)
}
for _, v := range valToCount {
for _, x := range gaussRange {
addend := int(round(normPDF(0, float64(x), float64(width))))
if addend <= 0 {
continue
}
if _, ok := newValToCount[v+x]; !ok {
newValToCount[v+x] = 0
}
newValToCount[v+x] += addend
}
}
// normalize
total := 0
for v := range newValToCount {
total += newValToCount[v]
}
probs := make(map[int]float64)
for v := range newValToCount {
probs[v] = float64(newValToCount[v]) / float64(total)
}
// return probability
if v, ok := probs[val]; ok {
P = v
}
// TODO: cache it
return
}
func normPDF(mean, x, sd float64) float64 {
m := sd * math.Sqrt(2*math.Pi)
e := math.Exp(-math.Pow(x-mean, 2) / (2 * math.Pow(sd, 2)))
return e / m
}
// https://play.golang.org/p/BkdofAFOJRh
func round(val float64) (newVal float64) {
roundOn := 0.5
places := 0
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}