-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbayes_test.go
85 lines (67 loc) · 1.56 KB
/
bayes_test.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
package bayes
import (
"log"
"os"
"testing"
)
func Example() {
data, labels := generateData()
nb := New()
if err := nb.Fit(data, labels); err != nil {
log.Printf("Failed to fit data: %+v", err)
}
for idx, label := range labels {
prediction := nb.Predict(data[idx])
log.Printf("Expected: %s Got: %s", label, prediction)
}
}
func TestNew(t *testing.T) {
if nb := New(); nb == nil {
t.Errorf("Failed to create new NaiveBayes")
}
}
func TestFit(t *testing.T) {
nb := New()
d, l := generateData()
if err := nb.Fit(d, l); err != nil {
t.Errorf("Failed to fit data: %+v", err)
}
}
func TestPredict(t *testing.T) {
nb := New()
d, l := generateData()
if err := nb.Fit(d, l); err != nil {
t.Errorf("Failed to fit data: %+v", err)
}
if p := nb.Predict([]float64{6.0}); p != "b" {
t.Errorf("Failed to predict the correct result: %s", p)
}
}
func TestDump(t *testing.T) {
nb := New()
d, l := generateData()
if err := nb.Fit(d, l); err != nil {
t.Errorf("Failed to fit data: %+v", err)
}
f, err := os.Create("/tmp/test-dump")
if err != nil {
t.Errorf("Failde to create tmp file: %+v", err)
}
err = nb.Dump(f)
if err != nil {
t.Errorf("Failde to create tmp file: %+v", err)
}
}
func TestLoad(t *testing.T) {
f, err := os.Open("/tmp/test-dump")
if err != nil {
t.Errorf("Failed to open dump file: %+v", err)
}
_, err = Load(f)
if err != nil {
t.Errorf("Failed to load from dump file: %+v", err)
}
}
func generateData() ([][]float64, []string) {
return [][]float64{{1.0}, {2.0}, {5.0}, {6.0}, {10.0}}, []string{"a", "a", "b", "b", "c"}
}