forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMean.scala
61 lines (56 loc) · 2 KB
/
KMean.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
50
51
52
53
54
55
56
57
58
59
60
61
// Wei Chen - K-Mean
// 2015-12-18
package com.scalaml.algorithm
import com.scalaml.general.MatrixFunc._
class KMean() extends Clustering {
val algoname: String = "KMean"
val version: String = "0.1"
var centers = Array[(Array[Double], Int)]()
var groupdata = Array[(Array[Double], Int)]()
var k = 2
var iter = 100
override def clear(): Boolean = {
centers = Array[(Array[Double], Int)]()
groupdata = Array[(Array[Double], Int)]()
k = 2
iter = 100
true
}
override def config(paras: Map[String, Any]): Boolean = try {
k = paras.getOrElse("K", paras.getOrElse("k", 2)).asInstanceOf[Int]
iter = paras.getOrElse("ITERATION", paras.getOrElse("iteration", paras.getOrElse("iter", 2))).asInstanceOf[Int]
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
// --- Start K-Mean Function ---
override def cluster( // K Mean
data: Array[Array[Double]] // Data Array(xi)
): Array[Int] = { // Return centers
centers = data.zipWithIndex
.groupBy { l => l._2 % k + 1 }
.map { l =>
val datasize = l._2.size
(matrixaccumulate(l._2.map(_._1)).map(_/datasize), l._1)
}.toArray
var i = 0
while (i < iter) {
groupdata = data.map { d =>
(d, centers.map { c =>
(c._2, arrayminussquare(d, c._1).sum)
}.minBy(_._2)._1)
}
val tempcenters = groupdata.groupBy(_._2).map { l =>
val datasize = l._2.size
(matrixaccumulate(l._2.map(_._1)).map(_/datasize), l._1)
}.toArray.sortBy(_._2)
if (centers.zip(tempcenters).map { l =>
if (l._1._2 == l._2._2) arrayequal(l._1._1, l._2._1)
else false
}.reduceLeft(_ && _)) i = iter
else centers = tempcenters
}
return groupdata.map(_._2)
}
}