-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMultipleLinearRegression.scala
74 lines (68 loc) · 2.29 KB
/
MultipleLinearRegression.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
62
63
64
65
66
67
68
69
70
71
72
73
74
// Wei Chen - Multiple Linear Regression
// 2019-05-27
package com.scalaml.algorithm
import com.scalaml.general.MatrixFunc._
class MultipleLinearRegression() extends Regression {
val algoname: String = "MultipleLinearRegression"
val version: String = "0.1"
var slope = Array[Double]()
var bias = Array[Double]()
override def clear(): Boolean = {
slope = Array[Double]()
bias = Array[Double]()
true
}
override def config(paras: Map[String, Any]): Boolean = {
true
}
// --- Start Multiple 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 xMean = matrixaccumulate(data.map(_._2)).map(_ / dataSize)
val yMean = data.map(_._1).sum / dataSize
val xSize = data.head._2.size
slope = new Array[Double](xSize)
bias = new Array[Double](xSize)
val tmpAccu = data.map { case (y, x) =>
val dy = y - yMean
val dx = arrayminus(x, xMean)
(dx.map(_ * dy), dx.map(v => Math.pow(v, 2)))
}
val numerator = matrixaccumulate(tmpAccu.map(_._1))
val denominator = matrixaccumulate(tmpAccu.map(_._2))
for(i <- 0 until xSize) {
slope(i) = numerator(i) / denominator(i)
bias(i) = yMean - slope(i) * xMean(i)
}
val loss = new Array[Double](xSize)
data.map { case (y, x) =>
for(i <- 0 until xSize) {
loss(i) += Math.pow((slope(i) * x(i) + bias(i)) - y, 2)
}
}
val lossSum = loss.sum
val equality = loss.map(l => lossSum - l)
val equSum = equality.sum
for(i <- 0 until xSize) {
slope(i) *= equality(i) / equSum
bias(i) *= equality(i) / equSum
}
true
} catch { case e: Exception =>
Console.err.println(e)
false
}
// --- Predict Multiple Linear Regression ---
override def predict(
data: Array[Array[Double]]
): Array[Double] = {
return data.map { d =>
val dSize = d.size
(for(i <- 0 until dSize) yield {
slope(i) * d(i) + bias(i)
}).sum
}
}
}