-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmultisvc_test.go
105 lines (84 loc) · 2.8 KB
/
multisvc_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
package ensemble
import (
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/evaluation"
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"testing"
)
func TestMultiSVMUnweighted(t *testing.T) {
Convey("Loading data...", t, func() {
inst, err := base.ParseCSVToInstances("../examples/datasets/articles.csv", false)
So(err, ShouldBeNil)
X, Y := base.InstancesTrainTestSplit(inst, 0.4)
m := NewMultiLinearSVC("l1", "l2", true, 1.0, 1e-4, nil)
m.Fit(X)
Convey("Predictions should work...", func() {
predictions, err := m.Predict(Y)
So(err, ShouldEqual, nil)
cf, err := evaluation.GetConfusionMatrix(Y, predictions)
So(err, ShouldEqual, nil)
So(evaluation.GetAccuracy(cf), ShouldBeGreaterThan, 0.70)
})
Convey("Saving should work...", func() {
f, err := ioutil.TempFile("", "tree")
So(err, ShouldBeNil)
err = m.Save(f.Name())
So(err, ShouldBeNil)
Convey("Loading should work...", func() {
mLoaded := NewMultiLinearSVC("l1", "l2", true, 1.00, 1e-8, nil)
err := mLoaded.Load(f.Name())
So(err, ShouldBeNil)
Convey("Predictions should be the same...", func() {
originalPredictions, err := m.Predict(Y)
So(err, ShouldBeNil)
newPredictions, err := mLoaded.Predict(Y)
So(err, ShouldBeNil)
So(base.InstancesAreEqual(originalPredictions, newPredictions), ShouldBeTrue)
})
})
})
})
}
func TestMultiSVMWeighted(t *testing.T) {
Convey("Loading data...", t, func() {
weights := make(map[string]float64)
weights["Finance"] = 0.1739
weights["Tech"] = 0.0750
weights["Politics"] = 0.4928
inst, err := base.ParseCSVToInstances("../examples/datasets/articles.csv", false)
So(err, ShouldBeNil)
X, Y := base.InstancesTrainTestSplit(inst, 0.4)
m := NewMultiLinearSVC("l1", "l2", true, 0.62, 1e-4, weights)
m.Fit(X)
Convey("Predictions should work...", func() {
predictions, err := m.Predict(Y)
So(err, ShouldEqual, nil)
cf, err := evaluation.GetConfusionMatrix(Y, predictions)
So(err, ShouldEqual, nil)
So(evaluation.GetAccuracy(cf), ShouldBeGreaterThan, 0.60)
Convey("Saving should work...", func() {
f, err := ioutil.TempFile("", "tree")
So(err, ShouldBeNil)
err = m.Save(f.Name())
So(err, ShouldBeNil)
Convey("Loading should work...", func() {
mLoaded := NewMultiLinearSVC("l1", "l2", true, 1.00, 1e-8, weights)
err := mLoaded.Load(f.Name())
So(err, ShouldBeNil)
Convey("Predictions should be the same...", func() {
originalPredictions, err := m.Predict(Y)
So(err, ShouldBeNil)
newPredictions, err := mLoaded.Predict(Y)
So(err, ShouldBeNil)
So(base.InstancesAreEqual(originalPredictions, newPredictions), ShouldBeTrue)
})
})
})
})
})
}
func TestMultiSVMSaved(t *testing.T) {
Convey("Loading data...", t, func() {
})
}