forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecisionTreeTest.scala
42 lines (33 loc) · 1.29 KB
/
DecisionTreeTest.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
// Wei Chen - Decision Tree Test
// 2016-11-24
import com.scalaml.TestData._
import com.scalaml.general.MatrixFunc._
import com.scalaml.algorithm.DecisionTree
import org.scalatest.funsuite.AnyFunSuite
class DecisionTreeSuite extends AnyFunSuite {
val dtree = new DecisionTree()
test("DecisionTree Test : Clear") {
assert(dtree.clear())
}
test("DecisionTree Test : Linear Data") {
assert(dtree.clear())
assert(dtree.config(Map[String, Double]()))
assert(dtree.train(LABELED_LINEAR_DATA))
val result = dtree.predict(UNLABELED_LINEAR_DATA)
assert(arrayequal(result, LABEL_LINEAR_DATA))
val treeString = dtree.tree.toString
assert(treeString == "col[0] >= 5.0 ? (class[1]) : (class[-1])")
}
test("DecisionTree Test : Nonlinear Data - WRONG") {
assert(dtree.clear())
assert(dtree.config(Map[String, Double]()))
assert(dtree.train(LABELED_NONLINEAR_DATA))
val result = dtree.predict(UNLABELED_NONLINEAR_DATA)
assert(!arrayequal(result, LABEL_NONLINEAR_DATA))
}
test("DecisionTree Test : Invalid Config & Data") {
assert(dtree.clear())
assert(!dtree.config(Map("maxLayer" -> "test")))
assert(!dtree.train(Array((1, Array(1, 2)), (1, Array()))))
}
}