Skip to content

Commit

Permalink
Breakable for loop to While loop
Browse files Browse the repository at this point in the history
  • Loading branch information
techaddict committed Apr 23, 2014
1 parent 39f85e0 commit 0a6d8e9
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.spark.mllib.tree

import scala.util.control.Breaks._

import org.apache.spark.annotation.Experimental
import org.apache.spark.{Logging, SparkContext}
import org.apache.spark.SparkContext._
Expand Down Expand Up @@ -82,32 +80,32 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
* still survived the filters of the parent nodes.
*/

// TODO: Convert for loop to while loop
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) {
// Extract info for nodes at the current level.
extractNodeInfo(nodeSplitStats, level, index, nodes)
// Extract info for nodes at the next lower level.
extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities,
filters)
logDebug("final best split = " + nodeSplitStats._1)
}
require(scala.math.pow(2, level) == splitsStatsForLevel.length)
// Check whether all the nodes at the current level at leaves.
val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0)
logDebug("all leaf = " + allLeaf)
if (allLeaf) break // no more tree construction
var level = 0
var break = false
while (level < maxDepth && !break) {

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) {
// Extract info for nodes at the current level.
extractNodeInfo(nodeSplitStats, level, index, nodes)
// Extract info for nodes at the next lower level.
extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities,
filters)
logDebug("final best split = " + nodeSplitStats._1)
}
require(scala.math.pow(2, level) == splitsStatsForLevel.length)
// Check whether all the nodes at the current level at leaves.
val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0)
logDebug("all leaf = " + allLeaf)
if (allLeaf) break = true // no more tree construction
else level += 1
}

// Initialize the top or root node of the tree.
Expand Down

0 comments on commit 0a6d8e9

Please sign in to comment.