forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultivariateLinearRegression.scala
53 lines (46 loc) · 1.58 KB
/
MultivariateLinearRegression.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
// Wei Chen - Multivariate Linear Regression - GD
// 2019-05-27
// Use Gradient Decent instead of the optimal solution
package com.scalaml.algorithm
import com.scalaml.general.MatrixFunc._
class MultivariateLinearRegression() extends Regression {
val algoname: String = "MultivariateLinearRegression"
val version: String = "0.1"
var weights = Array[Double]()
var limit = 1000 // for GD
var lr = 0.01 // for GD
override def clear(): Boolean = {
weights = Array[Double]()
true
}
override def config(paras: Map[String, Any]): Boolean = try {
limit = paras.getOrElse("LIMIT", paras.getOrElse("limit", 1000)).asInstanceOf[Int]
lr = paras.getOrElse("learning_rate", paras.getOrElse("lr", 0.01)).asInstanceOf[Double]
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
// --- Start Multivariate Linear Regression Function ---
override def train(
data: Array[(Double, Array[Double])] // Data Array(yi, xi)
): Boolean = try { // Return PData Class
val dataSize = data.size
val y = data.map(_._1)
val x = data.map(_._2 :+ 1.0)
val xSize = x.head.size
weights = gradientDescent(x, y, lr, limit)
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
// --- Predict Multivariate Linear Regression ---
override def predict(
data: Array[Array[Double]]
): Array[Double] = {
return data.map { d =>
(d :+ 1.0).zip(weights).map { case (x, w) => w * x }.sum
}
}
}