-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNaiveBoost.scala
35 lines (28 loc) · 1.07 KB
/
NaiveBoost.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
// Wei Chen - Naive Boost
// 2018-09-26
package com.scalaml.algorithm
class NaiveBoost() extends Classification {
val algoname: String = "NaiveBoost"
val version: String = "0.1"
var classifiers = Array[Classification]()
override def clear(): Boolean = {
classifiers = Array[Classification]()
true
}
override def config(paras: Map[String, Any]): Boolean = try {
classifiers = paras.getOrElse("CLASSIFIERS", paras.getOrElse("classifiers", Array(new BayesianDecision): Any)).asInstanceOf[Array[Classification]]
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
override def train(data: Array[(Int, Array[Double])]): Boolean = {
classifiers.forall(classifier => classifier.train(data))
}
override def predict(data: Array[Array[Double]]): Array[Int] = {
val results = classifiers.map(classifier => classifier.predict(data))
(for(i <- 0 until data.size) yield {
results.map(_(i)).groupBy(identity).maxBy(_._2.size)._1
}).toArray
}
}