-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Wei Chen - Gradient Boost | ||
// 2022-12-27 | ||
|
||
package com.scalaml.algorithm | ||
import com.scalaml.general.MatrixFunc._ | ||
|
||
class GradientBoost() extends Regression { | ||
val algoname: String = "GradientBoost" | ||
val version: String = "0.1" | ||
|
||
var regressors = Array[Regression]() | ||
|
||
override def clear(): Boolean = { | ||
regressors = Array[Regression]() | ||
true | ||
} | ||
|
||
override def config(paras: Map[String, Any]): Boolean = try { | ||
regressors = paras.getOrElse("REGRESSORS", paras.getOrElse("regressors", Array(new StochasticGradientDecent): Any)).asInstanceOf[Array[Regression]] | ||
true | ||
} catch { case e: Exception => | ||
Console.err.println(e) | ||
false | ||
} | ||
|
||
override def train(data: Array[(Double, Array[Double])]): Boolean = { | ||
var check = regressors.size > 0 | ||
var residue = Array.fill(data.size)(0.0) | ||
for (regressor <- regressors) { | ||
val tmpdata = data.zip(residue).map { case (d, r) => (d._1 + r, d._2) } | ||
check &= regressor.train(tmpdata) | ||
residue = arrayminus(data.map(_._1), regressor.predict(data.map(_._2))) | ||
} | ||
check | ||
} | ||
|
||
override def predict(data: Array[Array[Double]]): Array[Double] = { | ||
val results = regressors.map(regressor => regressor.predict(data)) | ||
matrixaccumulate(results).map(_ / regressors.size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/scala/algorithm/regression/GradientBoostTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Wei Chen - Gradient Boost Test | ||
// 2022-12-27 | ||
|
||
import com.scalaml.TestData._ | ||
import com.scalaml.general.MatrixFunc._ | ||
import com.scalaml.algorithm._ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class GradientBoostSuite extends AnyFunSuite { | ||
|
||
val gb = new GradientBoost() | ||
|
||
test("GradientBoost Test : Clear") { | ||
assert(gb.clear()) | ||
} | ||
|
||
test("GradientBoost Test : Linear Data") { | ||
assert(gb.clear()) | ||
assert(gb.config(Map[String, Any]())) | ||
assert(gb.train(LABELED_LINEAR_DATA.map(yx => yx._1.toDouble -> yx._2))) | ||
val result = gb.predict(UNLABELED_LINEAR_DATA) | ||
val nResult = result.map(v => if (v > 0) 1.0 else -1.0) | ||
assert(arraysimilar(nResult, LABEL_LINEAR_DATA.map(_.toDouble), 0.9)) | ||
} | ||
|
||
test("GradientBoost Test : Nonlinear Data, 1 Linear Model - WRONG") { | ||
assert(gb.clear()) | ||
assert(gb.config(Map[String, Any]())) | ||
assert(gb.train(LABELED_NONLINEAR_DATA.map(yx => yx._1.toDouble -> yx._2))) | ||
val result = gb.predict(UNLABELED_NONLINEAR_DATA) | ||
assert(!arraysimilar(result, LABEL_NONLINEAR_DATA.map(_.toDouble), 0.45)) | ||
} | ||
|
||
// More linear regressors will not solve nonlinear problems | ||
test("GradientBoost Test : Nonlinear Data, 5 Linear Models - WRONG") { | ||
val regressors: Any = Array( | ||
new StochasticGradientDecent, | ||
new StochasticGradientDecent, | ||
new StochasticGradientDecent, | ||
new StochasticGradientDecent, | ||
new StochasticGradientDecent | ||
) | ||
assert(gb.clear()) | ||
assert(gb.config(Map("regressors" -> regressors))) | ||
assert(gb.train(LABELED_NONLINEAR_DATA.map(yx => yx._1.toDouble -> yx._2))) | ||
val result = gb.predict(UNLABELED_NONLINEAR_DATA) | ||
assert(!arraysimilar(result, LABEL_NONLINEAR_DATA.map(_.toDouble), 0.45)) | ||
} | ||
|
||
test("GradientBoost Test : Invalid Config & Data") { | ||
assert(gb.clear()) | ||
assert(!gb.config(Map("regressors" -> "test"))) | ||
assert(!gb.train(Array((1, Array(1, 2)), (1, Array())))) | ||
} | ||
} |