-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKNN.scala
48 lines (42 loc) · 1.37 KB
/
KNN.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
// Wei Chen - K-Nearest-Neighborhood
// 2015-12-21
package com.scalaml.algorithm
class KNN() extends Classification {
val algoname: String = "KNN"
val version: String = "0.1"
var referencepoints = Array[(Int, Array[Double])]()
var k = 1
override def clear(): Boolean = {
referencepoints = Array[(Int, Array[Double])]()
k = 1
true
}
override def config(paras: Map[String, Any]): Boolean = try {
k = paras.getOrElse("K", paras.getOrElse("k", 1.0)).asInstanceOf[Double].toInt
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
// --- Start KNN Function ---
override def train(tdata: Array[(Int, Array[Double])]): Boolean = {
referencepoints = tdata
true
}
override def predict( // K Mean
pdata: Array[Array[Double]] // Data Array(xi)
): Array[Int] = { // Return PData Class
return pdata.map { pd =>
referencepoints.map { td =>
(td._1, td._2.zip(pd).map(l => Math.pow(l._1 - l._2, 2)).sum)
}.sortBy(_._2)
.take(k)
.groupBy(_._1)
.map(l => (l._2.size, l._2(0)._2, l._1))
.toArray
.sortBy(_._2)
.reverse
.maxBy(_._1)._3
}
}
}