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

    https://github.com/apache/spark/pull/79#discussion_r10360441
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala ---
    @@ -0,0 +1,915 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.tree
    +
    +import org.apache.spark.SparkContext._
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.mllib.tree.model._
    +import org.apache.spark.{SparkContext, Logging}
    +import org.apache.spark.mllib.regression.LabeledPoint
    +import org.apache.spark.mllib.tree.model.Split
    +import scala.util.control.Breaks._
    +import org.apache.spark.mllib.tree.configuration.Strategy
    +import org.apache.spark.mllib.tree.configuration.QuantileStrategy._
    +import org.apache.spark.mllib.tree.configuration.FeatureType._
    +import org.apache.spark.mllib.tree.configuration.Algo._
    +import org.apache.spark.mllib.tree.impurity.Impurity
    +
    +/**
    +A class that implements a decision tree algorithm for classification and 
regression.
    +It supports both continuous and categorical features.
    +
    +@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.
    + */
    +class DecisionTree private (val strategy : Strategy) extends Serializable 
with Logging {
    +
    +  /**
    +  Method to train a decision tree model over an RDD
    +
    +  @param input RDD of [[org.apache.spark.mllib.regression.LabeledPoint]] 
used as training data
    +  for DecisionTree
    +  @return a DecisionTreeModel that can be used for prediction
    +   */
    +  def train(input : RDD[LabeledPoint]) : DecisionTreeModel = {
    +
    +    //Cache input RDD for speedup during multiple passes
    +    input.cache()
    +
    +    val (splits, bins) = DecisionTree.findSplitsBins(input, strategy)
    +    logDebug("numSplits = " + bins(0).length)
    +    strategy.numBins = bins(0).length
    +
    +    val maxDepth = strategy.maxDepth
    +
    +    val maxNumNodes = scala.math.pow(2,maxDepth).toInt - 1
    +    val filters = new Array[List[Filter]](maxNumNodes)
    +    filters(0) = List()
    +    val parentImpurities = new Array[Double](maxNumNodes)
    +    //Dummy value for top node (updated during first split calculation)
    +    //parentImpurities(0) = Double.MinValue
    +    val nodes = new Array[Node](maxNumNodes)
    +
    +    logDebug("algo = " + strategy.algo)
    +
    +    breakable {
    +      for (level <- 0 until maxDepth){
    +
    +        logDebug("#####################################")
    +        logDebug("level = " + level)
    +        logDebug("#####################################")
    +
    +        //Find best split for all nodes at a level
    +        val splitsStatsForLevel = DecisionTree.findBestSplits(input, 
parentImpurities, strategy,
    +          level, filters,splits,bins)
    +
    +        for ((nodeSplitStats, index) <- 
splitsStatsForLevel.view.zipWithIndex){
    +
    +          extractNodeInfo(nodeSplitStats, level,  index, nodes)
    +          extractInfoForLowerLevels(level, index, maxDepth, 
nodeSplitStats, parentImpurities,
    +            filters)
    +          logDebug("final best split = " + nodeSplitStats._1)
    +
    +        }
    +        require(scala.math.pow(2,level)==splitsStatsForLevel.length)
    +
    +        val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0 )
    +        logDebug("all leaf = " + allLeaf)
    +        if (allLeaf) break
    +
    +      }
    +    }
    +
    +    val topNode = nodes(0)
    +    topNode.build(nodes)
    +
    +    val decisionTreeModel = {
    +      return new DecisionTreeModel(topNode, strategy.algo)
    +    }
    +
    +    return decisionTreeModel
    +  }
    +
    +
    +  private def extractNodeInfo(
    +      nodeSplitStats: (Split, InformationGainStats),
    +      level: Int, index: Int,
    +      nodes: Array[Node]) {
    +
    +    val split = nodeSplitStats._1
    +    val stats = nodeSplitStats._2
    +    val nodeIndex = scala.math.pow(2, level).toInt - 1 + index
    +    val isLeaf = (stats.gain <= 0) || (level == strategy.maxDepth - 1)
    +    val node = new Node(nodeIndex, stats.predict, isLeaf, Some(split), 
None, None, Some(stats))
    +    logDebug("Node = " + node)
    +    nodes(nodeIndex) = node
    +  }
    +
    +  private def extractInfoForLowerLevels(
    +      level: Int,
    +      index: Int,
    +      maxDepth: Int,
    +      nodeSplitStats: (Split, InformationGainStats),
    +      parentImpurities: Array[Double],
    +      filters: Array[List[Filter]]) {
    +
    +    for (i <- 0 to 1) {
    +
    +      val nodeIndex = scala.math.pow(2, level + 1).toInt - 1 + 2 * index + 
i
    +
    +      if (level < maxDepth - 1) {
    +
    +        val impurity = if (i == 0) {
    +          nodeSplitStats._2.leftImpurity
    +        } else {
    +          nodeSplitStats._2.rightImpurity
    +        }
    +
    +        logDebug("nodeIndex = " + nodeIndex + ", impurity = " + impurity)
    +        parentImpurities(nodeIndex) = impurity
    +        val childFilter = new Filter(nodeSplitStats._1, if (i == 0) -1 
else 1)
    +        filters(nodeIndex) = childFilter :: filters((nodeIndex - 1) / 2)
    +
    +        for (filter <- filters(nodeIndex)) {
    +          logDebug("Filter = " + filter)
    +        }
    +
    +      }
    +    }
    +  }
    +}
    +
    +object DecisionTree extends Serializable with Logging {
    +
    +  /**
    +  Method to train a decision tree model over an RDD
    +
    +  @param input RDD of [[org.apache.spark.mllib.regression.LabeledPoint]] 
used as training data
    +  for DecisionTree
    +  @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 a DecisionTreeModel that can be used for prediction
    +  */
    +  def train(input : RDD[LabeledPoint], strategy : Strategy) : 
DecisionTreeModel = {
    +    new DecisionTree(strategy).train(input : RDD[LabeledPoint])
    +  }
    +
    +  /**
    +  Method to train a decision tree model over an RDD
    +
    +  @param input RDD of [[org.apache.spark.mllib.regression.LabeledPoint]] 
used as training data
    +  for DecisionTree
    +  @param algo classification or regression
    +  @param impurity criterion used for information gain calculation
    +  @param maxDepth maximum depth of the tree
    +  @return a DecisionTreeModel that can be used for prediction
    +    */
    +  def train(
    +             input : RDD[LabeledPoint],
    --- End diff --
    
    Use 2-space indentation.


---
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.
---

Reply via email to