-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclassifier_test.go
132 lines (107 loc) · 3.39 KB
/
classifier_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
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
package mockingbird_test
import (
"github.com/lazywei/liblinear"
. "github.com/lazywei/mockingbird"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Naive Bayes", func() {
X, y := liblinear.ReadLibsvm("test_fixture/test_samples.libsvm", false)
/* _, nFeatures := X.Dims() */
/* X, _ = X.View(0, 0, 3, nFeatures).(*mat64.Dense) */
/* y, _ = y.View(0, 0, 3, 1).(*mat64.Dense) */
nb := NewNaiveBayes()
Describe("Fit", func() {
nb.Fit(X, y)
tokensTotal, langsTotal, langsCount, tokensTotalPerLang, tokenCountPerLang := nb.GetParams()
It("should count tokens and languages", func() {
Expect(tokensTotal).To(Equal(238))
Expect(langsTotal).To(Equal(22))
})
It("should count samples for each languages", func() {
Expect(langsCount).To(Equal(map[int]int{
0: 2, 1: 2, 2: 1,
3: 1, 4: 2, 5: 3,
6: 1, 7: 1, 8: 1,
9: 4, 10: 4}))
})
It("should count total number of tokens for each languages", func() {
Expect(tokensTotalPerLang).To(Equal(map[int]int{
0: 24, 1: 31, 2: 96,
3: 6, 4: 20, 5: 33,
6: 5, 7: 2, 8: 2,
9: 12, 10: 7,
}))
})
It("should count number of each token for each languages", func() {
expected := []int{
1, 1, 1, 1, 3, 3, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0}
for i, expectedVal := range expected {
Expect(tokenCountPerLang[0][i]).To(Equal(expectedVal))
}
expected = []int{
0, 0, 2, 1, 2, 2, 2, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1,
2, 1, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0}
for i, expectedVal := range expected {
Expect(tokenCountPerLang[5][i]).To(Equal(expectedVal))
}
})
})
Describe("Prediction", func() {
nb.Fit(X, y)
It("should predict", func() {
expectedPreds := []struct {
label int
score float64
}{
{0, -40.298975165660956},
{0, -29.496302349153602},
{1, -44.92853869111085},
{1, -53.84420594344641},
{2, -291.5183688683531},
{3, -11.069010546486865},
{4, -35.37466680850959},
{4, -10.468801361586188},
{5, -26.093289645514158},
{5, -13.493553760768126},
{5, -67.38900486121871},
{6, -11.138232015528818},
{7, -4.477336814478206},
{8, -4.477336814478206},
{9, -12.337521871950372},
{9, -8.466320861042481},
{9, -4.882801922586371},
{9, -4.882801922586371},
{10, -4.210274029229161},
{10, -2.264363880173848},
{10, -4.210274029229161},
{10, -4.210274029229161},
}
for i, pred := range nb.Predict(X) {
Expect(pred.Label).To(Equal(expectedPreds[i].label))
Expect(pred.Score).To(BeNumerically("~", expectedPreds[i].score))
}
})
})
Describe("ToGob and NewNaiveBayesFromGob", func() {
nb.Fit(X, y)
It("should be the same after load from encoded gob", func() {
gobStr := nb.ToGob()
nnb := NewNaiveBayesFromGob(gobStr)
a1, b1, c1, d1, e1 := nb.GetParams()
a2, b2, c2, d2, e2 := nnb.GetParams()
Expect(a1).To(Equal(a2))
Expect(b1).To(Equal(b2))
Expect(c1).To(Equal(c2))
Expect(d1).To(Equal(d2))
Expect(e1).To(Equal(e2))
})
})
})