forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSVMTest.scala
49 lines (43 loc) · 1.72 KB
/
LinearSVMTest.scala
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
// Wei Chen - Linear SVM Test
// 2016-06-03
import com.scalaml.TestData._
import com.scalaml.general.MatrixFunc._
import com.scalaml.algorithm.LinearSVM
import org.scalatest.funsuite.AnyFunSuite
class LinearSVMSuite extends AnyFunSuite {
val linearsvm = new LinearSVM()
test("LinearSVM Test : Clear") {
assert(linearsvm.clear())
assert(linearsvm.projector.isEmpty)
}
test("LinearSVM Test : Linear Data") {
assert(linearsvm.clear())
assert(linearsvm.projector.isEmpty)
val cost = Map(-1 -> 1.0, 1 -> 1.0)
val limit = 1000
val err = 1e-1
val paras: Map[String, Any] = Map("cost" -> cost, "limit" -> limit, "err" -> err)
assert(linearsvm.config(paras))
assert(linearsvm.train(LABELED_LINEAR_DATA))
assert(!linearsvm.projector.isEmpty)
val result = linearsvm.predict(UNLABELED_LINEAR_DATA)
assert(arrayequal(result, LABEL_LINEAR_DATA))
}
test("LinearSVM Test : Nonlinear Data - WRONG") {
val cost = Map(1 -> 1.0, 2 -> 1.0)
val limit = 1000
val err = 1e-1
val paras: Map[String, Any] = Map("cost" -> cost, "limit" -> limit, "err" -> err)
assert(linearsvm.clear())
assert(linearsvm.config(paras))
assert(linearsvm.train(LABELED_NONLINEAR_DATA))
assert(!linearsvm.projector.isEmpty)
val result = linearsvm.predict(UNLABELED_NONLINEAR_DATA)
assert(!arrayequal(result, LABEL_NONLINEAR_DATA))
}
test("LinearSVM Test : Invalid Config & Data") {
assert(linearsvm.clear())
assert(!linearsvm.config(Map("limit" -> "test")))
assert(!linearsvm.train(Array((1, Array(1, 2)), (1, Array(2)), (2, Array(1)), (2, Array()))))
}
}