forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussianProcess.scala
45 lines (37 loc) · 1.46 KB
/
GaussianProcess.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
// Wei Chen - Gaussian Process
// 2016-11-24
package com.scalaml.algorithm
import com.scalaml.general.MatrixFunc._
class GaussianProcess() extends Classification {
val algoname: String = "GaussianProcess"
val version: String = "0.1"
var pointGroups = Map[Int, Array[Array[Double]]]()
var std: Double = 1.0 // Standard Deviation
override def clear(): Boolean = {
pointGroups = Map[Int, Array[Array[Double]]]()
std = 1.0
true
}
override def config(paras: Map[String, Any]): Boolean = try {
std = paras.getOrElse("STANDARD_DEVIATION", paras.getOrElse("standard_deviation", paras.getOrElse("std", 1.0))).asInstanceOf[Double]
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
private def prob(a1: Array[Double], a2:Array[Double], s: Double): Double =
Math.exp(-arrayminussquare(a1, a2).sum / Math.pow(s, 2))
override def train(tdata: Array[(Int, Array[Double])]): Boolean = {
pointGroups = tdata.groupBy(_._1).map(l => (l._1, l._2.map(_._2)))
true
}
override def predict( // Gaussian Process
pdata: Array[Array[Double]], // Data Array(xi)
): Array[Int] = { // Return PData Class
return pdata.map { pd =>
pointGroups.map { gdata =>
(gdata._1, gdata._2.map(gd => prob(pd, gd, std)).sum)
}.maxBy(_._2)._1
}
}
}