Github user Ishiihara commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2394#discussion_r18107318
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/regression/StochasticGradientBoosting.scala
 ---
    @@ -0,0 +1,173 @@
    +package org.apache.spark.mllib.regression
    +
    +import org.apache.spark.mllib.linalg.Vector
    +import org.apache.spark.mllib.tree.DecisionTree
    +import org.apache.spark.mllib.tree.configuration.Algo.Algo
    +import org.apache.spark.mllib.tree.configuration.Strategy
    +import org.apache.spark.mllib.tree.impurity.Impurity
    +import org.apache.spark.mllib.tree.model.DecisionTreeModel
    +import org.apache.spark.rdd.{DoubleRDDFunctions, RDD}
    +import scala.util.Random
    +
    +/**
    + *
    + * Read about the algorithm "Gradient boosting" here:
    + * 
http://www.montefiore.ulg.ac.be/services/stochastic/pubs/2007/GWD07/geurts-icml2007.pdf
    + *
    + * Libraries that implement the algorithm "Gradient boosting" similar way
    + * https://code.google.com/p/jforests/
    + * https://code.google.com/p/jsgbm/
    + *
    + */
    +class StochasticGradientBoosting {
    +
    +  /**
    +   * Train a Gradient Boosting model given an RDD of (label, features) 
pairs.
    +   *
    +   * @param input Training dataset: RDD of 
[[org.apache.spark.mllib.regression.LabeledPoint]].
    +   * @param leaningRate Learning rate
    +   * @param countOfTrees Number of trees.
    +   * @param samplingSizeRatio Size of random sample, percent of ${input} 
size.
    +   * @param strategy The configuration parameters for the tree algorithm 
which specify the type
    +   *                 of algorithm (classification, regression, etc.), 
feature type (continuous,
    +   *                 categorical), depth of the tree, quantile calculation 
strategy, etc.
    +   * @return StochasticGradientBoostingModel that can be used for 
prediction
    +   */
    +  def run(
    +       input : RDD[LabeledPoint],
    +       leaningRate : Double,
    +       countOfTrees : Int,
    +       samplingSizeRatio : Double,
    +       strategy: Strategy): StochasticGradientBoostingModel = {
    +
    +    val featureDimension = input.count()
    +    val mean = new DoubleRDDFunctions(input.map(l => l.label)).mean()
    +    val boostingModel = new StochasticGradientBoostingModel(countOfTrees, 
mean, leaningRate)
    +
    +    for (i <- 0 to countOfTrees - 1) {
    +      val gradient = input.map(l => l.label - 
boostingModel.computeValue(l.features))
    +
    +      val newInput: RDD[LabeledPoint] = input
    +        .zip(gradient)
    +        .map{case(inputVal, gradientVal) => new LabeledPoint(gradientVal, 
inputVal.features)}
    +
    +      val randomSample = newInput.sample(
    +        false,
    +        (samplingSizeRatio * featureDimension).asInstanceOf[Int],
    +        Random.nextInt()
    +      )
    +
    +      val model = DecisionTree.train(randomSample, strategy)
    +      boostingModel.addTree(model)
    +    }
    +    boostingModel
    +  }
    +}
    +
    +/**
    + * Model that can be used for prediction.
    + *
    + * @param countOfTrees Number of trees.
    + * @param initValue Initialize model with this value.
    + * @param learningRate Learning rate.
    + */
    +class StochasticGradientBoostingModel (
    +    private val countOfTrees: Int,
    +    private var initValue: Double,
    +    private val learningRate: Double) extends Serializable with 
RegressionModel {
    +
    +  val trees: Array[DecisionTreeModel] = new 
Array[DecisionTreeModel](countOfTrees)
    +  var index: Int = 0
    --- End diff --
    
    private


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to