-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKNNTest.scala
36 lines (30 loc) · 1018 Bytes
/
KNNTest.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
// Wei Chen - K Nearest Neighborhood Test
// 2016-06-04
import com.scalaml.TestData._
import com.scalaml.general.MatrixFunc._
import com.scalaml.algorithm.KNN
import org.scalatest.funsuite.AnyFunSuite
class KNNSuite extends AnyFunSuite {
val knn = new KNN()
test("KNN Test : Clear") {
assert(knn.clear())
}
test("KNN Test : Linear Data") {
assert(knn.clear())
assert(knn.config(Map("k" -> 3.0)))
assert(knn.train(LABELED_LINEAR_DATA))
val result = knn.predict(UNLABELED_LINEAR_DATA)
assert(arrayequal(result, LABEL_LINEAR_DATA))
}
test("KNN Test : Nonlinear Train") {
assert(knn.clear())
assert(knn.config(Map("k" -> 1.0)))
assert(knn.train(LABELED_NONLINEAR_DATA))
val result = knn.predict(UNLABELED_NONLINEAR_DATA)
assert(arrayequal(result, LABEL_NONLINEAR_DATA))
}
test("KNN Test : Invalid Config") {
assert(knn.clear())
assert(!knn.config(Map("k" -> "test")))
}
}